您好我正在使用spring jpa和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>
在上面的文件中我提到了消息转换器。但是当我调用POST服务请求时,它会给我一些值
服务器拒绝了此请求,因为请求实体的格式不受所请求方法所请求资源的支持。
我的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。
我保留了在调用Web服务时,在我的标题中Content-Type:application / json
。 任何人都可以帮助我理解为什么我的应用程序无法接受json数据。 我的控制器代码片段
@RequestMapping(value = "/saveTimings", method = RequestMethod.POST)
public @ResponseBody
Outlet saveTimings(@RequestBody Timing timing) throws Exception {
return timing;
}
答案 0 :(得分:1)
您已将Jackson转换器注册为错误的媒体类型
<beans:property name="supportedMediaTypes" value="application/json" />
而是添加<beans:property name="prefixJson" value="false"/>