springboot post接口接受json時,轉(zhuǎn)換為對象時,屬性都為null的解決
背景
在接口請求過程中,傳遞json對象,springboot轉(zhuǎn)換為實體VO對象后,所有屬性都為null。
post請求:
后臺接收請求:
當時就懵逼了…
解決心路歷程
查看springboot默認的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); } } }
默認的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時,用的轉(zhuǎn)換器應(yīng)該是MappingJackson2HttpMessageConverter。
進入MappingJackson2HttpMessageConverter進行debug調(diào)試,發(fā)現(xiàn),最后轉(zhuǎn)換結(jié)果還真是null!
嘗試直接用objectMapper轉(zhuǎn)換對象看一下結(jié)果
結(jié)果驚不驚喜,意不意外~。objectMapper把對象轉(zhuǎn)為json時,屬性變?yōu)?strong>下劃線+小寫風格了。
難怪對象的屬性都為null,壓根屬性都不是同一個了
看看jackson配置能不能配置轉(zhuǎn)換為駝峰
將命名策略修改為LOWER_CAMEL_CASE。
再看看轉(zhuǎn)換結(jié)果:
成功轉(zhuǎn)換為駝峰,對象屬性也完美賦值!
將springboot默認的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è)置時間格式 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序列化會優(yōu)先采用FastJsonHttpMessageConverter。
再次查看接口解析,發(fā)現(xiàn)直接轉(zhuǎn)換到了對象屬性中。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
從零搭建腳手架之集成Spring?Retry實現(xiàn)失敗重試和熔斷器模式(實戰(zhàn)教程)
在我們的大多數(shù)項目中,會有一些場景需要重試操作,而不是立即失敗,讓系統(tǒng)更加健壯且不易發(fā)生故障,這篇文章主要介紹了從零搭建開發(fā)腳手架之集成Spring?Retry實現(xiàn)失敗重試和熔斷器模式,需要的朋友可以參考下2022-07-07Springboot 整合shiro實現(xiàn)權(quán)限控制的方法
這篇文章主要介紹了Springboot 整合shiro實現(xiàn)權(quán)限控制的相關(guān)知識,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11SpringMVC中Controller類數(shù)據(jù)響應(yīng)的方法
這篇文章主要介紹了SpringMVC中的數(shù)據(jù)響應(yīng)的問題,主要來了解 Controller 類如何進行數(shù)據(jù)響應(yīng)的,本文給大家介紹的非常詳細,需要的朋友可以參考下2021-07-07Spring源碼如何修改Bean的屬性用到的相關(guān)類
這篇文章主要介紹了Spring源碼如何修改Bean的屬性用到的相關(guān)類,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05SpringBoot基于過濾器和內(nèi)存實現(xiàn)重復請求攔截功能
這篇文章主要介紹了SpringBoot基于過濾器和內(nèi)存實現(xiàn)重復請求攔截,這里我們使用過濾器的方式對進入服務(wù)器的請求進行過濾操作,實現(xiàn)對相同客戶端請求同一個接口的過濾,需要的朋友可以參考下2023-01-01解決SpringMVC同時接收Json和Restful時Request里有Map的問題
今天小編就為大家分享一篇解決SpringMVC同時接收Json和Restful時Request里有Map的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08spring cloud eureka微服務(wù)之間的調(diào)用詳解
這篇文章主要介紹了spring cloud eureka微服務(wù)之間的調(diào)用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07