spring boot 默认使用的json转换工具是jackson。
集成fastjson,pom.xml引入fastjson的jar
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.15</version> </dependency>
配置fastjson
启动类注入Bean HttpMessageConverters
@Bean public HttpMessageConverters fastjsonHttpMessageConverter(){ //定义一个转换消息的对象 FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); //添加fastjson的配置信息 比如 :是否要格式化返回的json数据 FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); //在转换器中添加配置信息 fastConverter.setFastJsonConfig(fastJsonConfig); HttpMessageConverter<?> converter = fastConverter; return new HttpMessageConverters(converter); }