SpringBoot配置FastJson并解决乱码问题
fastjson
FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
项目地址:https://gitcode.com/gh_mirrors/fastj/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原代码。
启动的时候走到这个断点。
看下具体的值,forceResponseEncoding 为false,这时候默认的返回编码就是系统的编码了。所以接下来把springboot的response编码设置为utf-8这个功能开启就好。
开启方式:
application.properties
#配置response编码
#spring.http.encoding.charset=UTF-8
#spring.http.encoding.enable=true
spring.http.encoding.force=true
GitHub 加速计划 / fastj / fastjson
25.69 K
6.51 K
下载
FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
最近提交(Master分支:3 个月前 )
c942c834 - 1 年前
5bc4709b - 1 年前
更多推荐
已为社区贡献7条内容
所有评论(0)