SpringBoot HttpMessageConverter消息轉(zhuǎn)換器的使用詳解
消息轉(zhuǎn)化器的作用
- 將請求報文轉(zhuǎn)化為Java對象
- 將Java對象轉(zhuǎn)化為響應(yīng)報文
消息轉(zhuǎn)化器的主要方法
- getSupportedMediaTypes:獲取支持的MediaType集合(如:text/html,text/plain,application/json)
- canRead:判斷是否能讀(請求)
- read:將請求數(shù)據(jù)進行格式轉(zhuǎn)換(canRead方法返回值為true時調(diào)用)
- canWrite:判斷是否能寫(響應(yīng))
- write:將響應(yīng)數(shù)據(jù)進行格式轉(zhuǎn)換(canWrite方法返回值為true時調(diào)用)
默認配置的消息轉(zhuǎn)化器
SpringMVC啟動時會自動配置一些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 | 負責讀、寫JSON格式數(shù)據(jù)(利用Jackson) |
| AllEncompassingFormHttpMessageConverter | 負責讀、寫Form表單數(shù)據(jù) |
| Jaxb2RootElementHttpMessageConverter | 負責讀、寫XML格式數(shù)據(jù)(使用JAXB) |
| ByteArrayHttpMessageConverter | 負責讀、寫二進制格式數(shù)據(jù) |
| StringHttpMessageConverter | 負責讀、寫字符串格式數(shù)據(jù) |
| ResourceHttpMessageConverter | 負責讀、寫資源文件數(shù)據(jù) |
| SourceHttpMessageConverter | 負責讀、寫資源數(shù)據(jù) |
注意事項
系統(tǒng)有默認配置的消息轉(zhuǎn)換器集合。
處理過程會按集合順序匹配合適的消息轉(zhuǎn)換器,如果有合適的,就會使用該消息轉(zhuǎn)換器處理(讀、寫),后續(xù)的消息轉(zhuǎn)換器不再執(zhí)行。
自定義的消息轉(zhuǎn)換器要想生效,必須放到集合中相同類型的消息轉(zhuǎn)換器前面,原因參考第二點。
思考:既然自定義的消息轉(zhuǎn)換器必須放到集合中相同類型的消息轉(zhuǎn)換器前面,那是否能直接改動集合中原有的消息轉(zhuǎn)換器來達到自定義的效果,而不必在加一個(暫未沒研究)。
添加自定義消息轉(zhuǎn)換器時注意默認消息轉(zhuǎn)換器是否生效
- WebMvcConfigurer.configureMessageConverters方法會覆蓋默認消息轉(zhuǎn)換器集合
- WebMvcConfigurer.extendMessageConverters方法不會覆蓋默認消息轉(zhuǎn)換器集合
到此這篇關(guān)于SpringBoot HttpMessageConverter消息轉(zhuǎn)換器的使用詳解的文章就介紹到這了,更多相關(guān)SpringBoot HttpMessageConverter內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot URL帶有特殊字符([]/{}等),報400錯誤的解決
這篇文章主要介紹了SpringBoot URL帶有特殊字符([]/{}等),報400錯誤的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
MyBatis-Plus中使用EntityWrappe進行列表數(shù)據(jù)倒序設(shè)置方式
這篇文章主要介紹了MyBatis-Plus中使用EntityWrappe進行列表數(shù)據(jù)倒序設(shè)置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
Mybatis報Type interface *.*Mapper is not&
本文主要介紹了Mybatis報Type interface *.*Mapper is not known to the MapperRegis,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07

