从所周知 spring boot 框架默认json解析为jackjson,若想使用第三方的json解析,该怎么办呢?
直接进入正题,在pom文件中加入如下配置。
com.alibaba fastjson 1.2.15
spring boot 启动类继承WebMvcConfigurerAdapter,然后重写configureMessageConverters此方法。
package com.demo;import com.alibaba.fastjson.serializer.SerializerFeature;import com.alibaba.fastjson.support.config.FastJsonConfig;import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.http.MediaType;import org.springframework.http.converter.HttpMessageConverter;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;import java.util.ArrayList;import java.util.List;/** * Created by Administrator on 2017/3/19. */@SpringBootApplicationpublic class Application extends WebMvcConfigurerAdapter { @Override public void configureMessageConverters(List> converters) { super.configureMessageConverters(converters); /* * 1. 需要先定义一个convert转换器对象 * 2. 配置添加fastjson的配置信息, 比如: 是否要格式化返回的json数据; * 3. 把配置信息添加到convert转换器对象中; * 4. 解决中文乱码 * 5. 将convert添加到转换器对象当中; */ FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig); //4. 解决中文乱码问题 List fastMediaTypes = new ArrayList (); fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes); converters.add(fastJsonHttpMessageConverter); } public static void main(String[] args) { SpringApplication.run(Application.class,args); }}