SpringBoot 配置FastJson

释放双眼,带上耳机,听听看~!

2019独角兽企业重金招聘Python工程师标准>>> SpringBoot 配置FastJson

SpringBoot 配置FastJson , Google百度一下一大堆 , 我就贴一下我的方式:

  • extends org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter

  • Override configureMessageConverters


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
1public class FastjsonConfig extends WebMvcConfigurerAdapter {
2
3    @Override
4    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
5        super.configureMessageConverters(converters);
6       //fastjson MessageConverter
7        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
8       //fastjson config
9        FastJsonConfig fastJsonConfig = new FastJsonConfig();
10        fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue);
11        
12        List<MediaType> fastMediaTypes = new ArrayList<>();
13      //MediaType : application/json;charset=UTF-8
14        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
15     
16        fastConverter.setSupportedMediaTypes(fastMediaTypes);
17        //fastjson config add to converter
18        fastConverter.setFastJsonConfig(fastJsonConfig);
19        
20        HttpMessageConverter<?> converter = fastConverter;
21      //String MessageConverter , charset : UTF-8
22        StringHttpMessageConverter stringConverter  = new StringHttpMessageConverter(Charset.forName("UTF-8"));
23      //remove response accept charset
24        stringConverter.setWriteAcceptCharset(false);
25      //config StringHttpMessageConverter MediaType
26        List<MediaType> stringSupportedMediaTypes = stringConverter.getSupportedMediaTypes();
27        //StringHttpMessageConverter MediaType : application/json;charset=UTF-8
28        stringSupportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
29        stringConverter.setSupportedMediaTypes(stringSupportedMediaTypes);
30
31        converters.add(stringConverter);
32        converters.add(converter);
33    }
34}
35
36
  1. FastJsonHttpMessageConverter 在FastJson包里,直接引入就可以了.
  2. fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); // 配置fastjson支持的mediaType为utf8,支持中文
  3. 配置StringHttpMessageConverter
  • 不配置StringHttpMessageConverter的话
    • return "abc";
    • 经过fastjson处理后 , 拿到的是 ""abc"";
  • 配置StringHttpMessageConverter 的 MediaType 为: application/json;charset=UTF-8, 支持中文
  • 将两个Converter添加到list

OVER !

转载于:https://my.oschina.net/ElEGenT/blog/2989822

给TA打赏
共{{data.count}}人
人已打赏
安全技术

C++调用Python

2022-1-11 12:36:11

安全漏洞

ISC BIND 9 递归查询远程拒绝服务漏洞

2011-11-18 11:12:22

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索