欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot HttpMessageConverter消息轉(zhuǎn)換器的使用詳解

 更新時(shí)間:2022年06月07日 11:03:27   作者:騎個(gè)小蝸牛  
在整個(gè)數(shù)據(jù)流轉(zhuǎn)過程中,前端的請(qǐng)求報(bào)文轉(zhuǎn)化為Java對(duì)象,Java對(duì)象轉(zhuǎn)化為響應(yīng)報(bào)文,這里就用到了消息轉(zhuǎn)換器HttpMessageConverter

消息轉(zhuǎn)化器的作用

  • 將請(qǐng)求報(bào)文轉(zhuǎn)化為Java對(duì)象
  • 將Java對(duì)象轉(zhuǎn)化為響應(yīng)報(bào)文

消息轉(zhuǎn)化器的主要方法

  • getSupportedMediaTypes:獲取支持的MediaType集合(如:text/html,text/plain,application/json)
  • canRead:判斷是否能讀(請(qǐng)求)
  • read:將請(qǐng)求數(shù)據(jù)進(jìn)行格式轉(zhuǎn)換(canRead方法返回值為true時(shí)調(diào)用)
  • canWrite:判斷是否能寫(響應(yīng))
  • write:將響應(yīng)數(shù)據(jù)進(jìn)行格式轉(zhuǎn)換(canWrite方法返回值為true時(shí)調(diào)用)

默認(rèn)配置的消息轉(zhuǎn)化器

SpringMVC啟動(dòng)時(shí)會(huì)自動(dòng)配置一些HttpMessageConverter(WebMvcConfigurationSupport類的addDefaultHttpMessageConverters)方法

源碼如下:

	protected final void addDefaultHttpMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
		messageConverters.add(new ByteArrayHttpMessageConverter());
		messageConverters.add(new StringHttpMessageConverter());
		messageConverters.add(new ResourceHttpMessageConverter());
		messageConverters.add(new ResourceRegionHttpMessageConverter());
		try {
			messageConverters.add(new SourceHttpMessageConverter<>());
		}
		catch (Throwable ex) {
			// Ignore when no TransformerFactory implementation is available...
		}
		messageConverters.add(new AllEncompassingFormHttpMessageConverter());
		if (romePresent) {
			messageConverters.add(new AtomFeedHttpMessageConverter());
			messageConverters.add(new RssChannelHttpMessageConverter());
		}
		if (jackson2XmlPresent) {
			Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.xml();
			if (this.applicationContext != null) {
				builder.applicationContext(this.applicationContext);
			}
			messageConverters.add(new MappingJackson2XmlHttpMessageConverter(builder.build()));
		}
		else if (jaxb2Present) {
			messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
		}
		if (jackson2Present) {
			Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.json();
			if (this.applicationContext != null) {
				builder.applicationContext(this.applicationContext);
			}
			messageConverters.add(new MappingJackson2HttpMessageConverter(builder.build()));
		}
		else if (gsonPresent) {
			messageConverters.add(new GsonHttpMessageConverter());
		}
		else if (jsonbPresent) {
			messageConverters.add(new JsonbHttpMessageConverter());
		}
		if (jackson2SmilePresent) {
			Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.smile();
			if (this.applicationContext != null) {
				builder.applicationContext(this.applicationContext);
			}
			messageConverters.add(new MappingJackson2SmileHttpMessageConverter(builder.build()));
		}
		if (jackson2CborPresent) {
			Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.cbor();
			if (this.applicationContext != null) {
				builder.applicationContext(this.applicationContext);
			}
			messageConverters.add(new MappingJackson2CborHttpMessageConverter(builder.build()));
		}
	}

部分消息轉(zhuǎn)換器解析

名稱描述
MappingJackson2HttpMessageConverter負(fù)責(zé)讀、寫JSON格式數(shù)據(jù)(利用Jackson)
AllEncompassingFormHttpMessageConverter負(fù)責(zé)讀、寫Form表單數(shù)據(jù)
Jaxb2RootElementHttpMessageConverter負(fù)責(zé)讀、寫XML格式數(shù)據(jù)(使用JAXB)
ByteArrayHttpMessageConverter負(fù)責(zé)讀、寫二進(jìn)制格式數(shù)據(jù)
StringHttpMessageConverter負(fù)責(zé)讀、寫字符串格式數(shù)據(jù)
ResourceHttpMessageConverter負(fù)責(zé)讀、寫資源文件數(shù)據(jù)
SourceHttpMessageConverter負(fù)責(zé)讀、寫資源數(shù)據(jù)

注意事項(xiàng)

系統(tǒng)有默認(rèn)配置的消息轉(zhuǎn)換器集合。

處理過程會(huì)按集合順序匹配合適的消息轉(zhuǎn)換器,如果有合適的,就會(huì)使用該消息轉(zhuǎn)換器處理(讀、寫),后續(xù)的消息轉(zhuǎn)換器不再執(zhí)行。

自定義的消息轉(zhuǎn)換器要想生效,必須放到集合中相同類型的消息轉(zhuǎn)換器前面,原因參考第二點(diǎn)。

思考:既然自定義的消息轉(zhuǎn)換器必須放到集合中相同類型的消息轉(zhuǎn)換器前面,那是否能直接改動(dòng)集合中原有的消息轉(zhuǎn)換器來達(dá)到自定義的效果,而不必在加一個(gè)(暫未沒研究)。

添加自定義消息轉(zhuǎn)換器時(shí)注意默認(rèn)消息轉(zhuǎn)換器是否生效

  • WebMvcConfigurer.configureMessageConverters方法會(huì)覆蓋默認(rèn)消息轉(zhuǎn)換器集合
  • WebMvcConfigurer.extendMessageConverters方法不會(huì)覆蓋默認(rèn)消息轉(zhuǎn)換器集合

到此這篇關(guān)于SpringBoot HttpMessageConverter消息轉(zhuǎn)換器的使用詳解的文章就介紹到這了,更多相關(guān)SpringBoot HttpMessageConverter內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論