继承WebMvcConfigurerAdapter,改写成自己的json转换工具的写法
1、建立实体类

1 package com.fastjson; 2 3 import com.alibaba.fastjson.annotation.JSONField; 4 5 import java.util.Date; 6 7 /** 8 * Created by liuya on 2018-01-17. 9 */ 10 public class UserPoJo 11 { 12 private int userId; 13 private String userName; 14 @JSONField(format="yyyy-MM-dd HH:mm:ss") 15 private Date createTime; 16 17 public Date getCreateTime() { 18 return createTime; 19 } 20 21 public void setCreateTime(Date createTime) { 22 this.createTime = createTime; 23 } 24 25 public int getUserId() { 26 return userId; 27 } 28 29 public void setUserId(int userId) { 30 this.userId = userId; 31 } 32 33 public String getUserName() { 34 return userName; 35 } 36 37 public void setUserName(String userName) { 38 this.userName = userName; 39 } 40 41 @Override 42 public String toString() { 43 return "UserPoJo{" + 44 "userId=" + userId + 45 ", userName='" + userName + '\'' + 46 ", createTime=" + createTime + 47 '}'; 48 } 49 }
注意:
@JSONField(format="yyyy-MM-dd HH:mm:ss")的使用是自定义格式
2、建立模拟服务器程序

1 package com.fastjson; 2 3 import com.alibaba.fastjson.serializer.SerializerFeature; 4 import com.alibaba.fastjson.support.config.FastJsonConfig; 5 import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; 6 import org.springframework.boot.SpringApplication; 7 import org.springframework.boot.autoconfigure.SpringBootApplication; 8 import org.springframework.http.MediaType; 9 import org.springframework.http.converter.HttpMessageConverter; 10 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 11 12 import java.util.ArrayList; 13 import java.util.List; 14 15 16 /** 17 * 模拟开启的服务器 18 */ 19 20 21 @SpringBootApplication 22 public class SpringboothelloApplication extends WebMvcConfigurerAdapter{ 23 24 /** 25 // * 在这里我们使用 @Bean注入 fastJsonHttpMessageConvert 26 // * @return 27 // */ 28 @Override 29 public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { 30 31 // 1、需要先定义一个 convert 转换消息的对象; 32 FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); 33 34 //2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据; 35 FastJsonConfig fastJsonConfig = new FastJsonConfig(); 36 fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); 37 38 //处理中文乱码 39 List<MediaType> fastMediaTypes = new ArrayList<>(); 40 fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); 41 fastConverter.setSupportedMediaTypes(fastMediaTypes); 42 43 //3、在convert中添加配置信息. 44 fastConverter.setFastJsonConfig(fastJsonConfig); 45 46 HttpMessageConverter<?> converter = fastConverter; 47 converters.add(fastConverter); 48 } 49 50 public static void main(String[] args) { 51 SpringApplication.run(SpringboothelloApplication.class, args); 52 } 53 54 }
注意:测试程序测试前启动
3、测试用的Controller

1 package com.fastjson; 2 3 import org.springframework.web.bind.annotation.RequestMapping; 4 import org.springframework.web.bind.annotation.RestController; 5 6 import java.util.Date; 7 8 /** 9 * Created by liuya on 2018-01-16. 10 * 11 * 测试用的一个helloworld例子 12 */ 13 14 15 @RestController 16 public class ControllerJson { 17 18 @RequestMapping("user") 19 public UserPoJo hello(){ 20 //实体类赋值 21 UserPoJo userPoJo = new UserPoJo(); 22 userPoJo.setUserId(111); 23 userPoJo.setUserName("王小二"); 24 userPoJo.setCreateTime(new Date()); 25 //返回实体类 26 return userPoJo; 27 } 28 }
4、测试结果
5、遇到问题字符编码错误
6、解决在SpringboothelloApplication中加入如下代码解决

1 //处理中文乱码 2 List<MediaType> fastMediaTypes = new ArrayList<>(); 3 fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); 4 fastConverter.setSupportedMediaTypes(fastMediaTypes);