1、配置pom.xml文档
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.easytest</groupId>
<artifactId>rebushu</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>rebushu</name>
<url>http://maven.apache.org</url>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency> <!--<!– spring boot devtools 依赖包. –>-->
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-devtools</artifactId>-->
<!--<optional>true</optional>-->
<!--<scope>true</scope>-->
<!--</dependency>-->
</dependencies> <!--构建节点-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--<configuration>-->
<!--<!–fork : 如果没有该项配置,肯呢个devtools不会起作用,即应用不会restart –>-->
<!--<fork>true</fork>-->
<!--</configuration>--> <dependencies>
<!--springloaded hot deploy -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.4.RELEASE</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions> </plugin> </plugins>
</build> </project>
2、书写测试代码pojo(实体类)
package com.easytest; import com.alibaba.fastjson.annotation.JSONField; import java.util.Date; /**
* Created by liuya on 2018-01-17.
*/
public class UserPoJo
{
private int userId;
private String userName;
@JSONField(format="yyyy-MM-dd HH:mm:ss")
private Date createTime; public Date getCreateTime() {
return createTime;
} public void setCreateTime(Date createTime) {
this.createTime = createTime;
} public int getUserId() {
return userId;
} public void setUserId(int userId) {
this.userId = userId;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} @Override
public String toString() {
return "UserPoJo{" +
"userId=" + userId +
", userName='" + userName + '\'' +
", createTime=" + createTime +
'}';
}
}
3、书写测试服务器
package com.easytest; import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import java.util.ArrayList;
import java.util.List; @SpringBootApplication
public class RebushuApplication extends WebMvcConfigurerAdapter { /**
// * 在这里我们使用 @Bean注入 fastJsonHttpMessageConvert
// * @return
// */
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { // 1、需要先定义一个 convert 转换消息的对象;
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); //2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); //处理中文乱码
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMediaTypes); //3、在convert中添加配置信息.
fastConverter.setFastJsonConfig(fastJsonConfig); HttpMessageConverter<?> converter = fastConverter;
converters.add(fastConverter);
} public static void main(String[] args) {
SpringApplication.run(RebushuApplication.class, args);
}
}
4、书写controller代码
package com.easytest; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.Date; /**
* Created by liuya on 2018-01-16.
*
* 测试用的一个helloworld例子
*/ @RestController
public class ControllerJson { @RequestMapping("user2")
public UserPoJo hello(){
//实体类赋值
UserPoJo userPoJo = new UserPoJo();
userPoJo.setUserId(111);
userPoJo.setUserName("王小二");
userPoJo.setCreateTime(new Date());
//返回实体类
return userPoJo;
}
}
5、编译器配置(详见:http://www.cnblogs.com/liuyangfirst/p/8318664.html)
6、maven启动,在编译器右侧栏点开右侧栏
7、访问网页测试一下是否联通
8、controller中加如下内容,然后F5刷新界面,如图就是成功实现热部署