fastjson轉換對象實體@JsonProperty不生效問題及解決
fastjson轉換對象實體@JsonProperty不生效
項目場景
請求第三方應用 返回json數(shù)據(jù)
問題描述
第三方返回的數(shù)據(jù)中,存在java關鍵詞,無法直接使用原屬性名進行對應 例如(class、interface等)使用@JsonProperty注解不能返回正確的結果
@Data
static class User{
? ? ?@JsonProperty( "class")
? ? ?private String userClass;
? ? ?@JsonProperty("interface")
? ? ?private String userInterface;
}
public static void main(String[] args) {
? ? Map<String,Object> map = new HashMap<>();
? ? map.put("class","測試");
? ? map.put("interface","測試1");
? ? String mapStr = JSONObject.toJSONString(map);
? ? System.out.println(mapStr);
? ? User user = JSONObject.parseObject(mapStr, User.class);
? ? System.out.println(user);
}正常情況來講 @JsonProperty 注解完全夠用,可以成功解析出想要的結果。
但往往事情并不是那么簡單
執(zhí)行結果 :
{"interface":"測試1","class":"測試"}
User(userClass=null, userInterface=null)
可以看出并沒有成功映射到想要的數(shù)據(jù)
原因分析
具體原因感興趣的同學可以看下 JSONObject.parseObject 的源碼
解決方案
解決方法有兩種
1、修改屬性名稱,使用原屬性名 + “_”
@Data
static class User{
? ? @JsonProperty( "class")
? ? private String class_;
? ?@JsonProperty("interface")
? ?private String interface_;
}
public static void main(String[] args) {
? ? Map<String,Object> map = new HashMap<>();
? ? map.put("class","測試");
? ? map.put("interface","測試1");
? ? String mapStr = JSONObject.toJSONString(map);
? ? System.out.println(mapStr);
? ? User user = JSONObject.parseObject(mapStr, User.class);
? ? System.out.println(user);
}執(zhí)行結果 :
{"interface":"測試1","class":"測試"}
User(class_=測試, interface_=測試1)
2、使用fastjson @JSONField注解
@Data
static class User{
@JSONField(name = "class")
private String userClass;
@JSONField(name = "interface")
private String userInterface;
}
public static void main(String[] args) {
? ? Map<String,Object> map = new HashMap<>();
? ? map.put("class","測試");
? ? map.put("interface","測試1");
? ? String mapStr = JSONObject.toJSONString(map);
? ? System.out.println(mapStr);
? ? User user = JSONObject.parseObject(mapStr, User.class);
? ? System.out.println(user);
}執(zhí)行結果:
{"interface":"測試1","class":"測試"}
User(userClass=測試, userInterface=測試1)
@JsonProperty 失效問題的排查
@JsonProperty 是Jackson提供的一個用于注解屬性、類、方法等的json注解。使用它可以改變Json序列化時屬性的名稱,一般默認使用屬性名,比如如下的代碼示例,如果沒有使用@JsonProperty注解那么id轉化為json為{“id”:11}.使用了則就是{“Id”:11}.
@JsonInclude(Include.NON_NULL)
public class User implements Serializable {
@JsonProperty("Id")
private Integer id;
@JsonProperty("Name")
private String name;
@JsonProperty("pwd")
private Integer passWord;
}在一次使用springboot項目時發(fā)現(xiàn)@JsonProperty不生效。
那么是因為啥呢?
因為在項目里還引用了fastJson,在debug時發(fā)現(xiàn)接口最后響應時是使用FastJson做json序列化。
解決方法:
使用@EnableWebMvc注解,加在啟動類上?;蛘咧苯釉陧椖坷锊灰胒astJson.
@EnableWebMvc
public class SpringBootMain extends SpringBootServletInitializer implements WebApplicationInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringBootMain.class);
}
}springboot 是如何選擇使用json序列化工具的呢?即如何調用jackson進行json序列化和反序列化?
springboot 通過HttpMessageConverters 消息轉換器通過jackson將java對象轉化為json字符串。如果項目里包含多個json工具包比如jackson ,fastjson,那么就會各個年級對象的內容選擇一個合適的去轉換為json。

這是HttpMessageConverters 消息轉換器所處的位置,所以項目里采用那個json工具由該類決定。
springboot默認使用jackson,springboot默認集成的就是jackson。
指定使用fastJson的一種做法:
public class SpringBootMain extends SpringBootServletInitializer implements WebApplicationInitializer {
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
// 1.定義一個converters轉換消息的對象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// 2.添加fastjson的配置信息,比如: 是否需要格式化返回的json數(shù)據(jù)
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// 3.在converter中添加配置信息
fastConverter.setFastJsonConfig(fastJsonConfig);
// 4.將converter賦值給HttpMessageConverter
HttpMessageConverter<?> converter = fastConverter;
// 5.返回HttpMessageConverters對象
return new HttpMessageConverters(converter);
}
}以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Spring中的注解之@Override和@Autowired
看別人寫的代碼,經(jīng)常會用到 @Override 和 @Autowired 這兩個注解.這邊總結一下這兩個注解的作用,對正在學習java的小伙伴們有很好地幫助,需要的朋友可以參考下2021-05-05
SpringCloud Alibaba 基本開發(fā)框架搭建過程
這篇文章主要介紹了SpringCloud Alibaba 基本開發(fā)框架搭建過程,開發(fā)工具選用的idea,本文通過圖文實例相結合給大家分享搭建全過程,需要的朋友可以參考下2021-06-06
教你使用java將excel數(shù)據(jù)導入MySQL
今天教大家如何使用Java將excel數(shù)據(jù)導入MySQL,文中有非常詳細的代碼示例,對正在學習java的小伙伴呢很有幫助,需要的朋友可以參考下2021-05-05

