当前位置:   article > 正文

spring boot 使用fastjson序列化对象_springboot fastjson序列化

springboot 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
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/1011251
推荐阅读
相关标签
  

闽ICP备14008679号