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

简介: 1.What:什么是SpringBoot?SpringBoot BannerSpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。

1.What:什么是SpringBoot?

img_1d01675edd8b007f6a88d7e6f24ba43d.png
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?如何操作呢?


img_e47cb4ee8a1385866ba901144e9a1b8b.png
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

目录
相关文章
|
4月前
|
Java 关系型数据库 MySQL
解决SpringBoot配置文件项目重启出现乱码的情况
解决SpringBoot配置文件项目重启出现乱码的情况
53 0
|
6月前
|
缓存 数据可视化 NoSQL
【异常】springboot集成@Cacheable缓存乱码的问题解决方案
【异常】springboot集成@Cacheable缓存乱码的问题解决方案
156 1
|
5月前
|
Java Windows
springboot打成jar包,在windows上运行出现乱码
springboot打成jar包,在windows上运行出现乱码
|
10月前
|
Java Spring
SpringBoot 问题解决之控制台启动乱码
SpringBoot 问题解决之控制台启动乱码
708 0
|
fastjson Java 数据格式
springboot使用fastjson格式化日期数据不生效
springboot使用fastjson格式化日期数据不生效
springboot使用fastjson格式化日期数据不生效
|
JSON fastjson Java
SpringBoot 使用 FastJson 返回 JSON 数据时过滤部分字段
SpringBoot 使用 FastJson 返回 JSON 数据时过滤部分字段
455 0
|
JSON fastjson Java
SpringBoot 使用Fastjson 将返回值进行格式化处理
SpringBoot 使用Fastjson 将返回值进行格式化处理
447 0
|
Java
springboot全局字符编码设置(解决乱码问题)
springboot全局字符编码设置(解决乱码问题)
1202 0
springboot全局字符编码设置(解决乱码问题)
|
Java Windows
SpringBoot打包之后乱码
SpringBoot打包之后乱码
168 0