最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

fastjson轉(zhuǎn)換對(duì)象實(shí)體@JsonProperty不生效問(wèn)題及解決

 更新時(shí)間:2022年08月30日 09:37:57   作者:碼農(nóng)晴明_  
這篇文章主要介紹了fastjson轉(zhuǎn)換對(duì)象實(shí)體@JsonProperty不生效問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

fastjson轉(zhuǎn)換對(duì)象實(shí)體@JsonProperty不生效

項(xiàng)目場(chǎng)景

請(qǐng)求第三方應(yīng)用 返回json數(shù)據(jù)

問(wèn)題描述

第三方返回的數(shù)據(jù)中,存在java關(guān)鍵詞,無(wú)法直接使用原屬性名進(jìn)行對(duì)應(yīng) 例如(class、interface等)使用@JsonProperty注解不能返回正確的結(jié)果

@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","測(cè)試");
? ? map.put("interface","測(cè)試1");
? ? String mapStr = JSONObject.toJSONString(map);
? ? System.out.println(mapStr);
? ? User user = JSONObject.parseObject(mapStr, User.class);
? ? System.out.println(user);
}

正常情況來(lái)講 @JsonProperty 注解完全夠用,可以成功解析出想要的結(jié)果。

但往往事情并不是那么簡(jiǎn)單

執(zhí)行結(jié)果 :

{"interface":"測(cè)試1","class":"測(cè)試"}

User(userClass=null, userInterface=null)

可以看出并沒(méi)有成功映射到想要的數(shù)據(jù)

原因分析

具體原因感興趣的同學(xué)可以看下 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","測(cè)試");
? ? map.put("interface","測(cè)試1");
? ? String mapStr = JSONObject.toJSONString(map);
? ? System.out.println(mapStr);
? ? User user = JSONObject.parseObject(mapStr, User.class);
? ? System.out.println(user);
}

執(zhí)行結(jié)果 :

{"interface":"測(cè)試1","class":"測(cè)試"}

User(class_=測(cè)試, interface_=測(cè)試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","測(cè)試");
? ? map.put("interface","測(cè)試1");
? ? String mapStr = JSONObject.toJSONString(map);
? ? System.out.println(mapStr);
? ? User user = JSONObject.parseObject(mapStr, User.class);
? ? System.out.println(user);
}

執(zhí)行結(jié)果:

{"interface":"測(cè)試1","class":"測(cè)試"}

User(userClass=測(cè)試, userInterface=測(cè)試1)

@JsonProperty 失效問(wèn)題的排查

@JsonProperty 是Jackson提供的一個(gè)用于注解屬性、類、方法等的json注解。使用它可以改變Json序列化時(shí)屬性的名稱,一般默認(rèn)使用屬性名,比如如下的代碼示例,如果沒(méi)有使用@JsonProperty注解那么id轉(zhuǎn)化為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項(xiàng)目時(shí)發(fā)現(xiàn)@JsonProperty不生效。

那么是因?yàn)樯赌兀?/strong>

因?yàn)樵陧?xiàng)目里還引用了fastJson,在debug時(shí)發(fā)現(xiàn)接口最后響應(yīng)時(shí)是使用FastJson做json序列化。

解決方法:

使用@EnableWebMvc注解,加在啟動(dòng)類上?;蛘咧苯釉陧?xiàng)目里不引用fastJson.

@EnableWebMvc
public class SpringBootMain extends SpringBootServletInitializer implements WebApplicationInitializer {
 
   @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringBootMain.class);
    }
}

springboot 是如何選擇使用json序列化工具的呢?即如何調(diào)用jackson進(jìn)行json序列化和反序列化?

springboot 通過(guò)HttpMessageConverters 消息轉(zhuǎn)換器通過(guò)jackson將java對(duì)象轉(zhuǎn)化為json字符串。如果項(xiàng)目里包含多個(gè)json工具包比如jackson ,fastjson,那么就會(huì)各個(gè)年級(jí)對(duì)象的內(nèi)容選擇一個(gè)合適的去轉(zhuǎn)換為json。

這是HttpMessageConverters 消息轉(zhuǎn)換器所處的位置,所以項(xiàng)目里采用那個(gè)json工具由該類決定。

springboot默認(rèn)使用jackson,springboot默認(rèn)集成的就是jackson。

指定使用fastJson的一種做法:

 
public class SpringBootMain extends SpringBootServletInitializer implements WebApplicationInitializer {
    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        // 1.定義一個(gè)converters轉(zhuǎn)換消息的對(duì)象
    
        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對(duì)象
        return new HttpMessageConverters(converter);
    }
}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot多文件分布式上傳功能實(shí)現(xiàn)

    SpringBoot多文件分布式上傳功能實(shí)現(xiàn)

    本文詳細(xì)介紹了如何在SpringBoot中實(shí)現(xiàn)多文件分布式上傳,并用代碼給出了相應(yīng)的實(shí)現(xiàn)思路和實(shí)現(xiàn)步驟,感興趣的朋友跟隨小編一起看看吧
    2023-06-06
  • Spring中的注解之@Override和@Autowired

    Spring中的注解之@Override和@Autowired

    看別人寫(xiě)的代碼,經(jīng)常會(huì)用到 @Override 和 @Autowired 這兩個(gè)注解.這邊總結(jié)一下這兩個(gè)注解的作用,對(duì)正在學(xué)習(xí)java的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-05-05
  • 快速使用IDEA圖形化界面連接Phoenix的方法

    快速使用IDEA圖形化界面連接Phoenix的方法

    最近很多朋友跟小編留言如何使用IDEA圖形化界面連接Phoenix,在這小編就不一一回復(fù)大家了,今天抽空給大家整理一篇教程關(guān)于idea 圖形化界面連接Phoenix的相關(guān)知識(shí),需要的朋友快來(lái)學(xué)習(xí)下吧
    2021-05-05
  • MyBatis Excutor 攔截器的巧妙用法

    MyBatis Excutor 攔截器的巧妙用法

    這里要講的巧妙用法是用來(lái)實(shí)現(xiàn)在攔截器中執(zhí)行額外 MyBatis 現(xiàn)有方法的用法,并提供了解決攔截Executor時(shí)想要修改MappedStatement時(shí)解決并發(fā)的問(wèn)題。感興趣的朋友一起看看吧
    2017-10-10
  • SpringCloud Alibaba 基本開(kāi)發(fā)框架搭建過(guò)程

    SpringCloud Alibaba 基本開(kāi)發(fā)框架搭建過(guò)程

    這篇文章主要介紹了SpringCloud Alibaba 基本開(kāi)發(fā)框架搭建過(guò)程,開(kāi)發(fā)工具選用的idea,本文通過(guò)圖文實(shí)例相結(jié)合給大家分享搭建全過(guò)程,需要的朋友可以參考下
    2021-06-06
  • Java定時(shí)任務(wù)原理詳解

    Java定時(shí)任務(wù)原理詳解

    當(dāng)下,java編碼過(guò)程中,實(shí)現(xiàn)定時(shí)任務(wù)的方式主要以以下兩種為主:spring框架的@Scheduled和quzrtz框架。本文主要就二者的框架原理實(shí)現(xiàn)做一個(gè)入門(mén)引導(dǎo),為了解深層實(shí)現(xiàn)細(xì)節(jié)做一定的鋪墊
    2022-07-07
  • JPA之QueryDSL-JPA使用指南

    JPA之QueryDSL-JPA使用指南

    Springdata-JPA是對(duì)JPA使用的封裝,Querydsl-JPA也是基于各種ORM之上的一個(gè)通用查詢框架,使用它的API類庫(kù)可以寫(xiě)出Java代碼的sql,下面就來(lái)介紹一下JPA之QueryDSL-JPA使用指南
    2023-11-11
  • 深入理解java中的synchronized關(guān)鍵字

    深入理解java中的synchronized關(guān)鍵字

    這篇文章主要介紹了java中的synchronized關(guān)鍵字,有需要的朋友可以參考一下
    2013-12-12
  • MybatisPlus之時(shí)間處理問(wèn)題

    MybatisPlus之時(shí)間處理問(wèn)題

    在數(shù)據(jù)庫(kù)設(shè)計(jì)時(shí),阿里巴巴編碼規(guī)約建議使用gmt_create和gmt_modified命名時(shí)間字段,并設(shè)置為datetime類型,本文介紹了兩種自動(dòng)填充時(shí)間字段的實(shí)現(xiàn)方式:SQL級(jí)別和代碼級(jí)別(使用MyBatis?Plus),SQL級(jí)別通過(guò)設(shè)置默認(rèn)值和更新值為CURRENT_TIMESTAMP
    2024-09-09
  • 教你使用java將excel數(shù)據(jù)導(dǎo)入MySQL

    教你使用java將excel數(shù)據(jù)導(dǎo)入MySQL

    今天教大家如何使用Java將excel數(shù)據(jù)導(dǎo)入MySQL,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴呢很有幫助,需要的朋友可以參考下
    2021-05-05

最新評(píng)論

马关县| 三原县| 兴仁县| 弋阳县| 油尖旺区| 册亨县| 白城市| 邳州市| 长顺县| 皋兰县| 皋兰县| 拜泉县| 任丘市| 咸丰县| 乾安县| 平顶山市| 永登县| 慈溪市| 建水县| 芦溪县| 武定县| 库伦旗| 梁平县| 南澳县| 五寨县| 若羌县| 奎屯市| 普洱| 焦作市| 木兰县| 肇东市| 铜山县| 故城县| 米林县| 寿宁县| 青州市| 海原县| 康平县| 连山| 四子王旗| 乌拉特中旗|