SpringBoot HttpMessageConverter消息轉(zhuǎn)換器的使用詳解
消息轉(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)文章
SpringBoot URL帶有特殊字符([]/{}等),報(bào)400錯(cuò)誤的解決
這篇文章主要介紹了SpringBoot URL帶有特殊字符([]/{}等),報(bào)400錯(cuò)誤的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
MyBatis-Plus中使用EntityWrappe進(jìn)行列表數(shù)據(jù)倒序設(shè)置方式
這篇文章主要介紹了MyBatis-Plus中使用EntityWrappe進(jìn)行列表數(shù)據(jù)倒序設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Mybatis報(bào)Type interface *.*Mapper is not&
本文主要介紹了Mybatis報(bào)Type interface *.*Mapper is not known to the MapperRegis,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07
java編程實(shí)現(xiàn)郵件定時(shí)發(fā)送的方法
這篇文章主要介紹了java編程實(shí)現(xiàn)郵件定時(shí)發(fā)送的方法,涉及Java基于定時(shí)器實(shí)現(xiàn)計(jì)劃任務(wù)的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
參數(shù)校驗(yàn)Spring的@Valid注解用法解析
這篇文章主要介紹了參數(shù)校驗(yàn)Spring的@Valid注解用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08

