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)文章
史上最簡單的MyBatis動(dòng)態(tài)SQL入門示例代碼
動(dòng)態(tài)sql,可以根據(jù)用戶對(duì)字段選擇和輸入,動(dòng)態(tài)生成一條sql執(zhí)行。接下來通過本文給大家分享MyBatis動(dòng)態(tài)SQL入門示例代碼,一起看看吧2017-03-03IDEA將Maven項(xiàng)目中指定文件夾下的xml等文件編譯進(jìn)classes的方法
這篇文章主要介紹了IDEA將Maven項(xiàng)目中指定文件夾下的xml等文件編譯進(jìn)classes的方法,幫助大家更好的利用IDEA進(jìn)行Java的開發(fā)學(xué)習(xí),感興趣的朋友可以了解下2021-01-01SpringBoot與SpringSecurity整合方法附源碼
這篇文章主要介紹了SpringBoot與SpringSecurity整合,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01鴻蒙HarmonyOS App開發(fā)造輪子之自定義圓形圖片組件的實(shí)例代碼
這篇文章主要介紹了鴻蒙HarmonyOS App開發(fā)造輪子之自定義圓形圖片組件,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01