当前位置:   article > 正文

spring boot 使用fastjson序列化对象

spring boot 使用fastjson序列化对象

spring boot 使用fastjson序列化对象

方法一:
添加配置

//@SpringBootApplication
public class FastJsonAdapter extends WebMvcConfigurerAdapter {
	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		super.configureMessageConverters(converters);

		FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
		fastConverter.setFastJsonConfig(fastJsonConfig);

		converters.add(fastConverter);
	}

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

可使用@SpringBootApplication,也能够在启动时配置java

@SpringBootApplication
public class App {
	public static void main(String[] args) {
		Class<?>[] c = {App.class,FastJsonAdapter.class};
		SpringApplication.run(c, args);
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

测试
在实体中添加@JSONField看是否序列化spring

@JSONField(serialize = false)
若是不序列化说明集成fastjson成功json

方法二:
配置在App.java 启动类中

@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
   FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
   FastJsonConfig fastJsonConfig = new FastJsonConfig();
   fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
   fastConverter.setFastJsonConfig(fastJsonConfig);
   HttpMessageConverter<?> converter = fastConverter;
   return new HttpMessageConverters(converter);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/997907
推荐阅读
相关标签
  

闽ICP备14008679号