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
有什么线索吗?
推荐答案
我最终了解到问题是由RequestMappingHandlerAdapter
Bean
<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中的默认消息转换器


基础教程推荐
猜你喜欢
- 存储 20 位数字的数据类型 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13