java消息转换器在spring中不工作
您好,我正在使用SpringJPA和hibernate制作一个web应用程序。 在我的servlet中。xml文件我放置了以下代码行
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<beans:bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<beans:property name="prefixJson" value="true"/>
<beans:property name="supportedMediaTypes" value="application/json"/>
<beans:property name="objectMapper">
<beans:ref bean="JacksonObjectMapper" />
</beans:property>
</beans:bean>
</mvc:message-converters>
</mvc:annotation-driven>
<context:component-scan base-package="com.abc.*.controller" />
<util:properties id="portalProperties" location="classpath:portal.abc.properties"></util:properties>
<util:properties id="imageProperties" location="classpath:image.properties"></util:properties>
<tx:annotation-driven />
<task:executor id="asyncExecutor" pool-size="25"/>
<task:annotation-driven executor="asyncExecutor" />
<annotation-driven conversion-service="applicationConversionService" />
<resources mapping="/resources/**" location="/resources/" />
<beans:bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<beans:property name="mediaTypes">
<beans:map>
<beans:entry key="html" value="text/html"/>
<beans:entry key="json" value="application/json"/>
</beans:map>
</beans:property>
<beans:property name="viewResolvers">
<beans:list>
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
</beans:list>
</beans:property>
<beans:property name="defaultViews">
<beans:list>
<beans:bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
<beans:property name="prefixJson" value="true"/>
</beans:bean>
</beans:list>
</beans:property>
</beans:bean>
<beans:bean id="JacksonObjectMapper" class="com.fasterxml.jackson.databind.ObjectMapper" />
<beans:bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
<mvc:resources mapping="/images/**" location="/images/" />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/style.css" location="/style.css" />
<mvc:resources mapping="/**" location="/" />
<beans:bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
<beans:bean id="applicationConversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<beans:property name="converters">
<beans:list>
<beans:bean class="com.abc.util.StringTrimmingConverter"/>
</beans:list>
</beans:property>
</beans:bean>
<mvc:interceptors>
<beans:bean id="webContentInterceptor" class="org.springframework.web.servlet.mvc.WebContentInterceptor">
<beans:property name="cacheSeconds" value="0"/>
<beans:property name="useExpiresHeader" value="false"/>
<beans:property name="useCacheControlHeader" value="true"/>
<beans:property name="useCacheControlNoStore" value="true"/>
</beans:bean>
</mvc:interceptors>
在上面的文件中,我提到了消息转换器。但是,当我要求一个售后服务请求时,它给了我一些价值观
The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.
我的pom。xml代码片段
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.1.0</version>
</dependency>
我可以返回json,但无法在请求正文中接受json。 我一直保持着
Content-Type: application/json
调用web服务时在我的标题中。 任何人都可以帮助我理解为什么我的应用程序无法接受json数据。 我的控制器代码片段
@RequestMapping(value = "/saveTimings", method = RequestMethod.POST)
public @ResponseBody
Outlet saveTimings(@RequestBody Timing timing) throws Exception {
return timing;
}
# 1 楼答案
您已将Jackson转换器注册到错误的媒体类型,它应该是
而是,并添加
<beans:property name="prefixJson" value="false"/>