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

目录
相关文章
|
Java 关系型数据库 MySQL
解决SpringBoot配置文件项目重启出现乱码的情况
解决SpringBoot配置文件项目重启出现乱码的情况
679 0
|
JSON Java fastjson
微服务——SpringBoot使用归纳——Spring Boot返回Json数据及数据封装——使用 fastJson 处理 null
本文介绍如何使用 fastJson 处理 null 值。与 Jackson 不同,fastJson 需要通过继承 `WebMvcConfigurationSupport` 类并覆盖 `configureMessageConverters` 方法来配置 null 值的处理方式。例如,可将 String 类型的 null 转为 &quot;&quot;,Number 类型的 null 转为 0,避免循环引用等。代码示例展示了具体实现步骤,包括引入相关依赖、设置序列化特性及解决中文乱码问题。
949 0
|
缓存 数据可视化 NoSQL
【异常】springboot集成@Cacheable缓存乱码的问题解决方案
【异常】springboot集成@Cacheable缓存乱码的问题解决方案
734 1
|
自然语言处理 IDE Java
SpringBoot start.aliyun.com创建项目,解决properties乱码的问题
通过确保文件和开发环境的编码一致,配置 Maven 编码,设置 Spring Boot 应用和嵌入式服务器的编码,可以有效解决 properties 文件的乱码问题。以上步骤可以帮助开发者确保在 Spring Boot 项目中正确处理和显示多语言字符,避免因编码问题导致的乱码现象。
1350 5
|
easyexcel Java Maven
springboot使用EasyExcel导入导出填充,解决导出乱码问题(web)
springboot使用EasyExcel导入导出填充,解决导出乱码问题(web)
2700 5
|
JSON fastjson Java
Spring Boot Jackson 和Fast JSON 用哪个好啊 ?
【4月更文挑战第22天】
4155 1
|
JSON Java fastjson
Springboot 最细节全面的接口传参接参介绍,总有你喜欢的一种方式
Springboot 最细节全面的接口传参接参介绍,总有你喜欢的一种方式
1966 0
Springboot 最细节全面的接口传参接参介绍,总有你喜欢的一种方式
|
缓存 NoSQL Java
[原创]SpringBoot 2.x Redis缓存乱码问题/自定义SpringBoot-Cache序列化方式
[原创]SpringBoot 2.x Redis缓存乱码问题/自定义SpringBoot-Cache序列化方式
[原创]SpringBoot 2.x Redis缓存乱码问题/自定义SpringBoot-Cache序列化方式
|
Java Windows
springboot打成jar包,在windows上运行出现乱码
springboot打成jar包,在windows上运行出现乱码
1097 0
|
NoSQL Java Redis
Springboot集成redis和解决redis key乱码问题
今天在使用springboot整合redis时出现序列化乱码的问题,所以记录一下
1427 0