我有一个Spring MVC 3.2应用程序,它不接受来自浏览器的JSON POST请求。如果我使用像CocoaRest这样的工具,一切都很好。但是,当我使用Chrome://restclient/content/restclient.html之类的浏览器或工具时,我得到了
HTTP状态415 - 服务器因请求而拒绝了此请求 实体的格式不受所请求资源的支持 要求的方法。
这是我的配置:
@Bean
public ContentNegotiationManagerFactoryBean contentNegotiationManager() {
Properties properties = new Properties();
properties.setProperty( "xml", "application/xml" );
properties.setProperty( "json", "application/json" );
properties.setProperty( "html", "application/html" );
ContentNegotiationManagerFactoryBean contentNegotiationManager = new ContentNegotiationManagerFactoryBean();
contentNegotiationManager.setFavorParameter( true );
contentNegotiationManager.setMediaTypes( properties );
contentNegotiationManager.setIgnoreAcceptHeader( true );
contentNegotiationManager.setDefaultContentType( MediaType.APPLICATION_JSON );
return contentNegotiationManager;
}
和我的pom:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
这是我的控制器:
@Transactional
@ResponseBody
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)public EmployeeDTO addEmployee(@PathVariable String officeName, @RequestBody @Valid final Employee employee,
BindingResult result) {
List<String> errors = new ArrayList<String>();
if ( result.hasErrors() ) {
for ( ObjectError error : result.getAllErrors() ) {
errors.add( error.getDefaultMessage() );
}
throw new EmployeeSetupException( errors, "Employee could not be configured, please check your submitted values." );
}
...
}
这是我的要求:
{
"address" : {
"status" : null,
"city" : "Lakewood",
"addressId" : 1,
"seqId" : null,
"addressLine1" : "123 Maple Ave",
"zip" : "80111",
"addressLine2" : "Apt 101",
"zipLong" : null,
"addressLine3" : "room C ",
"state" : "CO"
},
"office" : null,
"licenseNumber" : "0987654321A",
"scheduleId" : null,
"user" : {
"status" : 1,
"userName" : "sid3@spistols.com",
"userRole" : {
"userRoleDescription" : "The person or entity that owns a particular practice, who has the highest level of authorization",
"userRoleDescriptionShort" : "Practice Owner",
"userRoleId" : 20
}
},
"firstName" : "Sid",
"ssn" : "111-22-3332",
"specialtyId" : null,
"lastName" : "Vicious",
"fullName" : null
}
我读了一些文章说将头文件内容设置为application / json或form会这样做。但是当我将contentType设置为application / json和/或application / x-www-form-urlencoded时,它仍然在浏览器中失败。我也试过通过jQuery提交,但我得到了相同的结果。
如果我从以下位置更改控制器:
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
到
@RequestMapping(method = RequestMethod.POST)
然后,当我通过浏览器提交时,我得到了这个:
Received Exception Content type 'text/plain;charset=UTF-8' not supported
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'text/plain;charset=UTF-8' not supported
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:149)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:180)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:95)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:123)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
这没有任何意义,因为Chrome rest客户端chrome://restclient/content/restclient.html显示了请求的applicationType / json的contentType。
如果有人知道可能出现的问题,我当然会感谢你的帮助。
答案 0 :(得分:0)
有两个原因:
检查数据模型中是否存在循环依赖关系(实体之间的关联)。如果是,请删除循环依赖。
由于您在控制器中指定@consumes,请确保在POST / PUT请求中提供Content-Type标头。