springboot利用fastjson序列化输出(默认是jackson)

Posted koushr

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot利用fastjson序列化输出(默认是jackson)相关的知识,希望对你有一定的参考价值。

在@SpringBootApplication类中添加

    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        //创建FastJson信息转换对象
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        //创建FastJson对象并设定序列化规则
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        fastJsonHttpMessageConverter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON_UTF8));
        //规则赋予转换对象
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
        return new HttpMessageConverters(fastJsonHttpMessageConverter, new StringHttpMessageConverter(Charset.forName("UTF-8")));
    }

 

以上是关于springboot利用fastjson序列化输出(默认是jackson)的主要内容,如果未能解决你的问题,请参考以下文章