SpringBoot配置FastJson并解决乱码问题

1.导入Jar包,我用的Gradle,导入方式如下(最后一个)

dependencies {
    //热部署
    compile("org.springframework.boot:spring-boot-devtools")
    //spring mvc
    compile("org.springframework.boot:spring-boot-starter-web")
    //设置程序监控
    compile("org.springframework.boot:spring-boot-starter-actuator")
    //数据库配置
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("com.alibaba:druid:1.0.23")
    compile("mysql:mysql-connector-java:$mysqlVersion")
    testCompile("org.springframework.boot:spring-boot-starter-test")
    //缓存
    compile("org.springframework.boot:spring-boot-starter-cache")
    //配置fastJson
    compile("com.alibaba:fastjson:1.2.24")
}

2.在项目中写一个配置类

package com.kingboy.springboot.config;

import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.kingboy.springboot.domain.JsonEntiry;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.nio.charset.Charset;

import static com.alibaba.fastjson.serializer.SerializerFeature.UseISO8601DateFormat;
import static com.alibaba.fastjson.serializer.SerializerFeature.WriteDateUseDateFormat;


@Configuration
public class FastJson{
    protected FastJson() {
    }

    @Bean
    public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();

        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.PrettyFormat,
                SerializerFeature.WriteClassName,//输出类名字
                SerializerFeature.WriteMapNullValue
        );
        ValueFilter valueFilter = new ValueFilter() {
            //o 是class
            //s 是key值
            //o1 是value值
            public Object process(Object o, String s, Object o1) {
                if (null == o1){
                    o1 = "";
                }
                return o1;
            }
        };
        fastJsonConfig.setSerializeFilters(valueFilter);
        //可以设置编码,默认UTF-8
        //fastJsonConfig.setCharset(Charset.forName("UTF-8"));

        converter.setFastJsonConfig(fastJsonConfig);

        return converter;
    }
}

3. 在配置文件中指定要使用的Json转换类

application.properties

#配置Json转换器
spring.http.converters.preferred-json-mapper=fastjson

实验了下,不导入这个配置,仍然会自动用fastJson代替Jackson,有知道原理的解惑下。

4.解决乱码

当我使用fastJson之后,返回页面的中文乱码,看了fastJson的源码默认使用UTF-8;

public class FastJsonHttpMessageConverter //
        extends AbstractHttpMessageConverter<Object> //
        implements GenericHttpMessageConverter<Object> {

    @Deprecated
    protected Charset charset = IOUtils.UTF8;

    @Deprecated
    protected SerializerFeature[] features = new SerializerFeature[0];

    @Deprecated
    protected SerializeFilter[] filters = new SerializeFilter[0];

    @Deprecated
    protected String dateFormat;

    ....

所以判断可能是springboot的返回编码不对。接着看springboot原代码。

启动的时候走到这个断点。

SpringBoot配置FastJson并解决乱码问题_第1张图片

看下具体的值,forceResponseEncoding 为false,这时候默认的返回编码就是系统的编码了。所以接下来把springboot的response编码设置为utf-8这个功能开启就好。

SpringBoot配置FastJson并解决乱码问题_第2张图片

开启方式:

application.properties

#配置response编码
#spring.http.encoding.charset=UTF-8
#spring.http.encoding.enable=true
spring.http.encoding.force=true

你可能感兴趣的:(☀框架技术)