javascript
SpringBoot自定义MessageConverter
例如引入xml依賴:
開啟協商功能:
spring:contentnegotiation:favor-parameter: true #開啟請求參數內容協商模式進行不同返回類型測試:
? 1、判斷當前響應頭中是否已經有確定的媒體類型。MediaType
? 2、獲取客戶端(PostMan、瀏覽器)支持接收的內容類型。(獲取客戶端Accept請求頭字段)【application/xml】
? contentNegotiationManager 內容協商管理器 默認使用基于請求頭的策略
? HeaderContentNegotiationStrategy 確定客戶端可以接收的內容類型
? 3、遍歷循環所有當前系統的 MessageConverter,看誰支持操作這個對象(Person)
? 4、找到支持操作Person的converter,把converter支持的媒體類型統計出來。
? 5、客戶端需要【application/xml】。服務端能力【10種、json、xml】
? 6、進行內容協商的最佳匹配媒體類型
? 7、用支持將對象轉為最佳匹配媒體類型 的converter。調用它進行轉化 。
上圖導入了jackson處理xml的包,xml的converter就會自動進來
WebMvcConfigurationSupport jackson2XmlPresent = ClassUtils.isPresent("com.fasterxml.jackson.dataformat.xml.XmlMapper", classLoader);if (jackson2XmlPresent) {Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.xml();if (this.applicationContext != null) {builder.applicationContext(this.applicationContext);}messageConverters.add(new MappingJackson2XmlHttpMessageConverter(builder.build()));}在開發過程中,我們可能對返回的數據有自己的要求
實現多協議數據兼容。json、xml、x-guigu
- @ResponseBody 響應數據出去 調用 RequestResponseBodyMethodProcessor 處理
- Processor 處理方法返回值。通過 MessageConverter 處理
- 所有 MessageConverter 合起來可以支持各種媒體類型數據的操作(讀、寫)
- 內容協商找到最終的 messageConverter;
SpringMVC的什么功能。一個入口給容器中添加一個 WebMvcConfigurer
注意:如果使用的是configureMessageConverter,那么是覆蓋了默認的所有messageconverter
自定義converter
將自定義的converter添加進去:
@Beanpublic WebMvcConfigurer webMvcConfigurer(){return new WebMvcConfigurer() {@Overridepublic void extendMessageConverters(List<HttpMessageConverter<?>> converters) {converters.add(new GuiguMessageConverter());}}}上面的方式是以請求頭的形式,進行內容協商的,如果以參數的形式,會方便一些。
我們可以看到,內容協商功能是由contentNegotiationManager實現的,在它下面有兩種策略:
- ParameterContentNegotiationStrategy(基于請求參數)
- HeadContentnegotiationStrategy(基于請求頭)
而請求參數的策略,默認只支持xml,json兩種方式,那么看到這里,思路自然也就有了,那就是自定義內容協商管理器contentNegotiationManager。
@Overridepublic void configureContentNegotiation(ContentNegotiationConfigurer configurer) {//Map<String, MediaType> mediaTypesMap<String, MediaType> mediaTypes = new HashMap<>();mediaTypes.put("json",MediaType.APPLICATION_JSON);mediaTypes.put("xml",MediaType.APPLICATION_XML);mediaTypes.put("gg",MediaType.parseMediaType("application/x-guigu"));//指定支持解析哪些參數對應的哪些媒體類型ParameterContentNegotiationStrategy parameterStrategy = new ParameterContentNegotiationStrategy(mediaTypes); // parameterStrategy.setParameterName("ff");HeaderContentNegotiationStrategy headeStrategy = new HeaderContentNegotiationStrategy();configurer.strategies(Arrays.asList(parameterStrategy,headeStrategy));}效果:
但是在這里要注意:如果我們自定義了參數策略,他的頭策略反而會失效,這時就需要debug了。。。。在spingboot中,有可能我們添加的自定義的功能會覆蓋默認很多功能,導致一些默認的功能失效,這里的解決方法是在configureContentNegotiation中也添加自定義頭策略.
總結
以上是生活随笔為你收集整理的SpringBoot自定义MessageConverter的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2021年KOL市场研究报告
- 下一篇: 2021年中国电子签名行业研究报告