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

springboot post接口接受json時(shí),轉(zhuǎn)換為對(duì)象時(shí),屬性都為null的解決

 更新時(shí)間:2021年10月19日 11:06:40   作者:專業(yè)矮矬窮  
這篇文章主要介紹了springboot post接口接受json時(shí),轉(zhuǎn)換為對(duì)象時(shí),屬性都為null的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

背景

在接口請(qǐng)求過程中,傳遞json對(duì)象,springboot轉(zhuǎn)換為實(shí)體VO對(duì)象后,所有屬性都為null。

post請(qǐng)求:

postman調(diào)用

后臺(tái)接收請(qǐng)求:

后臺(tái)接收請(qǐng)求

當(dāng)時(shí)就懵逼了…

解決心路歷程

查看springboot默認(rèn)的HttpMessageConverter

@Configuration
@Component
public class AppWebConfiguration implements WebMvcConfigurer {
	/**
	 * 重寫添加攔截器方法并添加配置攔截器
	 * 
	 * @param registry
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
	}
	@Override
	public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
		for (HttpMessageConverter<?> messageConverter : converters) {
			System.out.println(messageConverter); 
		}
	}
}

默認(rèn)的HttpMessageConverter如下:

org.springframework.http.converter.ByteArrayHttpMessageConverter@4ee488a7
org.springframework.http.converter.StringHttpMessageConverter@7c556701
org.springframework.http.converter.StringHttpMessageConverter@1650e1e1
org.springframework.http.converter.ResourceHttpMessageConverter@ffa6a44
org.springframework.http.converter.ResourceRegionHttpMessageConverter@65317aac
org.springframework.http.converter.xml.SourceHttpMessageConverter@328b7464
org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter@2345d43d
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@7f31325b
org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@113bd30b

所以解析jason時(shí),用的轉(zhuǎn)換器應(yīng)該是MappingJackson2HttpMessageConverter。

進(jìn)入MappingJackson2HttpMessageConverter進(jìn)行debug調(diào)試,發(fā)現(xiàn),最后轉(zhuǎn)換結(jié)果還真是null!

嘗試直接用objectMapper轉(zhuǎn)換對(duì)象看一下結(jié)果

結(jié)果驚不驚喜,意不意外~。objectMapper把對(duì)象轉(zhuǎn)為json時(shí),屬性變?yōu)?strong>下劃線+小寫風(fēng)格了。

難怪對(duì)象的屬性都為null,壓根屬性都不是同一個(gè)了

看看jackson配置能不能配置轉(zhuǎn)換為駝峰

將命名策略修改為L(zhǎng)OWER_CAMEL_CASE。

再看看轉(zhuǎn)換結(jié)果:

成功轉(zhuǎn)換為駝峰,對(duì)象屬性也完美賦值!

將springboot默認(rèn)的HttpMessageConverter替換為阿里的FastJson轉(zhuǎn)換器MappingJackson2HttpMessageConverter看看效果

@Configuration
public class FastJsonConfiguration {
    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        List<MediaType> fastMediaTypes = new ArrayList<>();
        // 處理中文亂碼問題
        fastJsonConfig.setCharset(Charset.forName("UTF-8"));
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        // 設(shè)置時(shí)間格式
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
        // 在轉(zhuǎn)換器中添加配置信息
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
        HttpMessageConverter converter = fastJsonHttpMessageConverter;
        StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
        stringConverter.setDefaultCharset(Charset.forName("UTF-8"));
        stringConverter.setSupportedMediaTypes(fastMediaTypes);
        return new HttpMessageConverters(stringConverter, converter);
    }
}

再次查看HttpMessageConverter如下:

com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter@15219255
org.springframework.http.converter.ByteArrayHttpMessageConverter@4ee488a7
org.springframework.http.converter.StringHttpMessageConverter@7c556701
org.springframework.http.converter.StringHttpMessageConverter@1650e1e1
org.springframework.http.converter.ResourceHttpMessageConverter@ffa6a44
org.springframework.http.converter.ResourceRegionHttpMessageConverter@65317aac
org.springframework.http.converter.xml.SourceHttpMessageConverter@328b7464
org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter@2345d43d
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@7f31325b
org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@113bd30b

發(fā)現(xiàn)FastJsonHttpMessageConverter已經(jīng)在MappingJackson2HttpMessageConverter之前,springboot序列化會(huì)優(yōu)先采用FastJsonHttpMessageConverter。

再次查看接口解析,發(fā)現(xiàn)直接轉(zhuǎn)換到了對(duì)象屬性中。

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

相關(guān)文章

  • Java實(shí)現(xiàn)遞歸讀取文件夾下的所有文件

    Java實(shí)現(xiàn)遞歸讀取文件夾下的所有文件

    這篇文章主要為大家詳細(xì)介紹了如何利用Java實(shí)現(xiàn)遞歸讀取文件夾下的所有文件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-02-02
  • 修改maven項(xiàng)目端口號(hào)的方法

    修改maven項(xiàng)目端口號(hào)的方法

    今天小編就為大家分享一篇修改maven項(xiàng)目端口號(hào)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • 一文詳解Spring攔截鏈的實(shí)現(xiàn)原理

    一文詳解Spring攔截鏈的實(shí)現(xiàn)原理

    在?Web應(yīng)用開發(fā)中,攔截器(Interceptor)是一種非常重要的機(jī)制,能夠在請(qǐng)求處理的各個(gè)階段進(jìn)行前置和后置處理,本文主要來探討一下?Spring?攔截鏈的實(shí)現(xiàn)原理,需要的可以了解下
    2025-01-01
  • Spring中的singleton和prototype的實(shí)現(xiàn)

    Spring中的singleton和prototype的實(shí)現(xiàn)

    這篇文章主要介紹了Spring中的singleton和prototype的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • 多模塊maven的deploy集成gitlab?ci自動(dòng)發(fā)版配置

    多模塊maven的deploy集成gitlab?ci自動(dòng)發(fā)版配置

    這篇文章主要為大家介紹了多模塊maven項(xiàng)目deploy集成gitlab?ci自動(dòng)發(fā)版的配置流程步驟,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2022-02-02
  • 詳解Spring Cloud 熔斷機(jī)制--斷路器

    詳解Spring Cloud 熔斷機(jī)制--斷路器

    這篇文章主要介紹了詳解Spring Cloud 熔斷機(jī)制--斷路器,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-04-04
  • Springboot+Redis執(zhí)行l(wèi)ua腳本的項(xiàng)目實(shí)踐

    Springboot+Redis執(zhí)行l(wèi)ua腳本的項(xiàng)目實(shí)踐

    本文主要介紹了Springboot+Redis執(zhí)行l(wèi)ua腳本的項(xiàng)目實(shí)踐,詳細(xì)的介紹Redis與Lua腳本的結(jié)合應(yīng)用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-09-09
  • 如何在Java中實(shí)現(xiàn)一個(gè)散列表

    如何在Java中實(shí)現(xiàn)一個(gè)散列表

    這篇文章主要介紹了如何在Java中實(shí)現(xiàn)一個(gè)散列表,建一個(gè)HashMap,以String類型為Key,Int類型為Value,下文具體的操作過程需要的小伙伴可以參考一下
    2022-04-04
  • 深入理解Java設(shè)計(jì)模式之簡(jiǎn)單工廠模式

    深入理解Java設(shè)計(jì)模式之簡(jiǎn)單工廠模式

    這篇文章主要介紹了JAVA設(shè)計(jì)模式之簡(jiǎn)單工廠模式的的相關(guān)資料,文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解下
    2021-11-11
  • 解決java文件流處理異常 mark/reset not supported問題

    解決java文件流處理異常 mark/reset not supported問題

    這篇文章主要介紹了解決java文件流處理異常 mark/reset not supported問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10

最新評(píng)論

阳原县| 舟山市| 利川市| 莆田市| 沁源县| 丹阳市| 巴塘县| 墨竹工卡县| 正安县| 专栏| 临猗县| 舞钢市| 梁河县| 宁夏| 虹口区| 潢川县| 汉川市| 会昌县| 陆丰市| 东兰县| 永昌县| 酉阳| 原平市| 怀柔区| 博乐市| 顺义区| 射洪县| 星子县| 松原市| 施甸县| 莒南县| 高碑店市| 固原市| 永仁县| 金华市| 和林格尔县| 宁河县| 蕲春县| 历史| 长沙县| 榆林市|