方法一、在启动类中配置
配置方式一(通过继承的方式)
1、启动类继承WebMvcConfigurerAdapter
2、重写configureMessageConverters方法
@SpringBootApplication @EnableDiscoveryClient @EnableScheduling public class MemberApplication extends WebMvcConfigurerAdapter { /** * 配置FastJson为方式一 * @return*/ @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { super.configureMessageConverters(converters); /* * 1、需要先定义一个convert转换消息的对象 2、添加fastJson的配置信息,比如:是否要格式化返回json数据 3、在convert中添加配置信息 * 4、将convert添加到converters当中 * */ // 1、需要先定义一个·convert转换消息的对象; FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); // 2、添加fastjson的配置信息,比如 是否要格式化返回json数据 FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); // 3、在convert中添加配置信息. fastConverter.setFastJsonConfig(fastJsonConfig); // 4、将convert添加到converters当中. converters.add(fastConverter); } public static void main(String[] args) { SpringApplication.run(MemberApplication.class, args); } }
注:开发中为了统一管理配置,可以放入配置类中,启动类只做启动的功能
@Configuration public class HttpConverterConfig { @Bean public HttpMessageConverters fastJsonHttpMessageConverters() { // 1.定义一个converters转换消息的对象 FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); // 2.添加fastjson的配置信息,比如: 是否需要格式化返回的json数据 FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); // 3.在converter中添加配置信息 fastConverter.setFastJsonConfig(fastJsonConfig); // 4.将converter赋值给HttpMessageConverter HttpMessageConverter<?> converter = fastConverter; // 5.返回HttpMessageConverters对象 return new HttpMessageConverters(converter); } }
2.2配置方式二(通过@Bean注入的方式)
在App.java启动类中,注入Bean : HttpMessageConverters
@SpringBootApplication @EnableDiscoveryClient @EnableScheduling public class MemberApplication { /** * 配置FastJson方式二 * @return HttpMessageConverters */ @Bean public HttpMessageConverters fastJsonHttpMessageConverters() { // 1.定义一个converters转换消息的对象 FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); // 2.添加fastjson的配置信息,比如: 是否需要格式化返回的json数据 FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); //字符类型字段如果为null,输出为"",而非null; fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteNullStringAsEmpty); //是否输出值为null的字段 fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue); //List字段如果为null,输出为[],而非null fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteNullListAsEmpty); //消除对同一对象循环引用的问题,默认为false(如果不配置有可能会进入死循环) fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect); //Boolean字段如果为null,输出为false,而非null fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteNullBooleanAsFalse); // 3.在converter中添加配置信息 fastConverter.setFastJsonConfig(fastJsonConfig); // 4.将converter赋值给HttpMessageConverter HttpMessageConverter<?> converter = fastConverter; // 5.返回HttpMessageConverters对象 return new HttpMessageConverters(converter); } public static void main(String[] args) { SpringApplication.run(MemberApplication.class, args); } }
在pojo类中:
private int id; private String name; //com.alibaba.fastjson.annotation.JSONField @JSONField(format="yyyy-MM-dd HH:mm") private Date createTime;//创建时间. /* * serialize:是否需要序列化属性. */ @JSONField(serialize=false) private String remarks;//备注信息.