Default Message Converters in Spring MVC 5(Spring MVC 5中的默认消息转换器)
本文介绍了Spring MVC 5中的默认消息转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试了解为什么我的spring v.5.0.4-RELEASE无法正确加载默认消息转换器。
我从我的servlet.xml中删除了所有声明,并希望找到从AbstractMessageConverterMethodProcessor中正确加载的所有默认转换器,但我只得到了以下4个:
org.springframework.http.converter.ByteArrayHttpMessageConverter@35ca138b
org.springframework.http.converter.StringHttpMessageConverter@2b755f0d
org.springframework.http.converter.xml.SourceHttpMessageConverter@74f5d717
org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter@6982b849
有什么线索吗?
推荐答案
我最终了解到问题是由RequestMappingHandlerAdapterBean
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
这是覆盖了Spring的默认设置,并发布了我的问题中列出的四个转换器。 解决方案是将我正在寻找的转换器放在Bean下面,如下所示:
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean
class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="autoDetectFields" value="true" />
<property name="autoDetectGettersSetters" value="false" />
<property name="objectMapper">
<bean class="com.mypackage.CustomMapper" />
</property>
</bean>
</property>
</bean>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
<bean class="org.springframework.http.converter.FormHttpMessageConverter"/>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
<value>text/html</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
放置在annotation-driven下的配置被完全忽略:
<mvc:annotation-driven>
<mvc:message-converters>
...
</mvc:message-converters>
</mvc:annotation-driven>
这篇关于Spring MVC 5中的默认消息转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
编程基础网
本文标题为:Spring MVC 5中的默认消息转换器
基础教程推荐
猜你喜欢
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
- JPA惰性列表上的流 2022-01-01
- 将 double 转换为 Int,向下舍入 2022-01-01
- 将 Windows 证书导入 Java 2022-01-01
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- Maven:无效的目标版本:10 2022-01-01
