且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SpringBoot换用FastJson:这是你喜欢的乱码~%?…,# *'☆&℃$︿★?

更新时间:2022-08-16 21:29:14

1.What:什么是SpringBoot?

SpringBoot换用FastJson:这是你喜欢的乱码~%?…,# *'☆&℃$︿★?
SpringBoot Banner

SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。

SpringBoot特性

1.创建独立的Spring应用程序

2.嵌入的Tomcat,无需部署WAR文件

3.简化Maven配置

4.自动配置Spring

5.提供生产就绪型功能,如指标,健康检查和外部配置

6.开箱即用,没有代码生成,也无需XML配置。

SpringBoot特性理解

•为基于Spring的开发提供更快的入门体验

•开箱即用,没有代码生成,也无需XML配置。同时也可以修改默认值来满足特定的需求。

•提供了一些大型项目中常见的非功能特性,如嵌入式服务器、安全、指标,健康检测、外部配置等。

•SpringBoot并不是对Spring功能上的增强,而是提供了一种快速使用Spring的方式。

2.Why+How:既然SpringBoot中已经集成jackson我们为什要用FastJson?如何操作呢?


SpringBoot换用FastJson:这是你喜欢的乱码~%?…,# *'☆&℃$︿★?
jackson相关依赖

{"age":18,"name":"小明同学","createTime":"2018-07-29T04:48:08.524+0000"}

通过查看Libraries我们可以得知,SpringBoot默认使用的jackson解析Json。现在我们要使用FastJson对jacjson进行替换。

<!--FastJson依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.15</version>
        </dependency>
<!--这里要说下很重要的话,官方文档说的1.2.10以后,会有两个方法支持HttpMessageconvert,一个是FastJsonHttpMessageConverter,支持4.2以下的版本,一个是FastJsonHttpMessageConverter4支持4.2以上的版本,具体有什么区别暂时没有深入研究。
这里也就是说:低版本的就不支持了,所以这里最低要求就是1.2.10+  -->

配置fastjson(支持两种办法)

第一种:
extends WebMvcConfigurationSupport()
override configureMessageConverters
注意:如果不配置中文乱码处理,显示效果会是酱紫的

{ "age":18, "createTime":"2018-07-29 14:54", "name":"灏忔槑鍚屽" }
@SpringBootApplication
public class SpringbootdemoApplication extends WebMvcConfigurationSupport {

    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        // 1、需要先定义一个 convert 转换消息的对象;
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        //2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
       
        //**处理中文乱码问题
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastConverter.setSupportedMediaTypes(fastMediaTypes);
        //3、在convert中添加配置信息.
        fastConverter.setFastJsonConfig(fastJsonConfig);
        //4、将convert添加到converters当中.
        converters.add(fastConverter);
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringbootdemoApplication.class, args);
    }
}
@JSONField(format = "yyyy-MM-dd HH:mm")
    private Date createTime;
<!-- 浏览器显示的json  时间格式已更改-->
{
    "age":18,
    "createTime":"2018-07-29 14:46",
    "name":"小明同学"
}

第二种:
使用 @Bean注入 fastJsonHttpMessageConvert

@Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        // 1、需要先定义一个 convert 转换消息的对象;
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

        //2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);

        //同样要处理中文乱码问题
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastConverter.setSupportedMediaTypes(fastMediaTypes);
        //3、在convert中添加配置信息.
        fastConverter.setFastJsonConfig(fastJsonConfig);


        HttpMessageConverter<?> converter = fastConverter;
        return new HttpMessageConverters(converter);
    }

所以为什么要换成fastjson呢?或许这就是爱吧~haha