激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術(shù)及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - springboot post接口接受json時,轉(zhuǎn)換為對象時,屬性都為null的解決

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

2022-02-23 00:32專業(yè)矮矬窮 Java教程

這篇文章主要介紹了springboot post接口接受json時,轉(zhuǎn)換為對象時,屬性都為null的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

背景

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

post請求:

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

后臺接收請求:

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

當時就懵逼了…

 

解決心路歷程

查看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)換器應該是MappingJackson2HttpMessageConverter。

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

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

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

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

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

難怪對象的屬性都為null,壓根屬性都不是同一個了

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

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

將命名策略修改為LOWER_CAMEL_CASE。

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

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

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

成功轉(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);
      // 設置時間格式
      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)驗,希望能給大家一個參考,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/jiangjun0130/article/details/89210172

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本黄色免费片 | 国产精品成人亚洲一区二区 | 久久精品成人影院 | 欧洲精品久久 | 综合欧美一区二区三区 | 717影院理论午夜伦八戒秦先生 | 午夜在线观看视频网站 | 亚洲国产色婷婷 | 91热久久免费频精品黑人99 | 久久午夜神器 | 美女在线视频一区二区 | 国产毛片网 | 欧美国产成人在线 | chinesexxxx刘婷hd 国产91在线播放九色 | 国产毛片自拍 | 男女一边摸一边做羞羞视频免费 | 一边吃奶一边摸下娇喘 | 视频一区国产 | 久久精品4 | www.xxx视频| 最新一区二区三区 | 成人性视频欧美一区二区三区 | 国产成视频在线观看 | av手机免费在线观看 | 奇米影视奇米色777欧美 | av在线免费观看网 | 久久久久国产视频 | 免费看日产一区二区三区 | 国产精品久久av | 国产一级做a爱片在线看免 2019天天干夜夜操 | 91麻豆精品国产91久久久无需广告 | 一级在线免费观看视频 | 女人a级毛片 | 性片久久 | 国产精品18久久久久久久 | 中文字幕观看 | 国产资源在线视频 | 一级黄色片武则天 | 亚洲国产高清视频 | 亚洲成人网一区 | 中文在线免费观看 |