一、去除原有的 Jackson 默認依賴法
json:一篇文章帶你深入分析 SpringBoot 中默認的 JSON 解析方式
gson:一篇文章帶你搞定 SpringBoot 如何使用 Gson
FastJSON 號稱是最快的JSON解析工具,但是和前面學習的 json 和 gson 相比,SpringBoot 並沒有給 Fastjson提供自動化的依賴配置,所以如果使用FastJson ,都需要自己額外配置
(1)排除掉默認的 json 依賴,加入 fastjson 依賴,注意需要指定版本
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.49</version>
</dependency>
二、示例展示
實體類 User:
package com.yolo.json.domain;
import java.util.Date;
public class User {
private Integer id;
private String name;
private String address;
private Date birthday;
//get,set 方法省略
UserController:
@RestController
public class UserController {
@GetMapping(value = "/user")
public User getUser(){
User user = new User();
user.setId(01);
user.setAddress("北京");
user.setName("Yolo");
user.setBirthday(new Date());
return user;
}
}
package com.yolo.json.config;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import java.util.ArrayList;
import java.util.List;
@Configuration
public class WebMvcConfig {
@Bean
FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
//1.定義一個convert 轉換消息對象
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
//2.定義一個config的配置信息,比如是否要格式化返回的json對象
FastJsonConfig config = new FastJsonConfig();
config.setSerializerFeatures(SerializerFeature.PrettyFormat);
//3.格式化返回日期
config.setDateFormat("yyyy-MM-dd");
//4.格式化返回的中文字符
List<MediaType> mediaTypeList = new ArrayList<>();
//注意這裏的APPLICATION_PROBLEM_JSON_UTF8過時了
mediaTypeList.add(MediaType.APPLICATION_JSON);
//5.添加配置
converter.setFastJsonConfig(config);
converter.setSupportedMediaTypes(mediaTypeList);
return converter;
}
}
這裏只需將 FastJsonHttpMessageConverter
加入容器即可,既可以替代默認的
三、直接覆蓋原有的 Jackson 法
(1)直接加入 fastjson 依賴即可,不用排除 默認的依賴
(2)重寫配置類,繼承 WebMvcConfigurer
,並重寫其中的 configureMessageConverters
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//1.定義一個convert 轉換消息對象
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
//2.定義一個config的配置信息,比如是否要格式化返回的json對象
FastJsonConfig config = new FastJsonConfig();
config.setSerializerFeatures(SerializerFeature.PrettyFormat);
//3.格式化返回日期
config.setDateFormat("yyyy-MM-dd");
//4.格式化返回的中文字符
List<MediaType> mediaTypeList = new ArrayList<>();
//注意這裏的APPLICATION_PROBLEM_JSON_UTF8過時了
mediaTypeList.add(MediaType.APPLICATION_JSON);
//5.添加配置
converter.setFastJsonConfig(config);
converter.setSupportedMediaTypes(mediaTypeList);
converters.add(converter);
}
}
四、中文亂碼解決方案
對於 FastJson 不對其進行格式字符編碼會出現中文字符亂碼現象
1. 模板格式化字符編碼
//4.格式化返回的中文字符
List<MediaType> mediaTypeList = new ArrayList<>();
//注意這裏的APPLICATION_PROBLEM_JSON_UTF8過時了
mediaTypeList.add(MediaType.APPLICATION_JSON);
//5.添加配置
converter.setSupportedMediaTypes(mediaTypeList);
具體代碼如示例中所示。
這裏需要注意一點就是以前使用的APPLICATION_PROBLEM_JSON_UTF8
,已經過時了,進入註解源碼可以發現
@Deprecated 註解的作用:
若某類或某方法加上該註解之後,表示此方法或類不再建議使用,調用時也會出現刪除線,但並不代表不能用,只是說,不推薦使用,因爲還有更好的方法可以調用
因爲在一個項目中,工程比較大,代碼比較多,而在後續開發過程中,可能之前的某個方法實現的並不是很合理,這個時候就要新加一個方法,而之前的方法又不能隨便刪除,因爲可能在別的地方有調用它,所以加上這個註解,就方便以後開發人員的方法調用了
所以這裏可以使用推薦的
2. 註解格式化字符編碼