2014-02-09 63 views
2

我有一个Spring MVC 3.2应用程序不接受来自浏览器的JSON POST请求。如果我使用CocoaRest等工具,一切都很好。但是,当我使用浏览器或工具,如铬://restclient/content/restclient.html然后我得到,因为请求 实体是不支持的格式SpringMVC JSON不受支持的类型

HTTP状态415 -The服务器拒绝这个请求请求的资源为 请求的方法。

这是我的配置:

@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" : "[email protected]", 
    "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 
    } 

我读了一些文章,说将标题内容设置为应用程序/ json或表单将做到这一点。但是,当我将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显示请求的application/json的contentType。

如果有人知道什么可能会出错,我当然会很感激帮助。

+0

你有没有尝试设置(在CONF文件):'contentNegotiationManager.setIgnoreAcceptHeader(假);' – nickdos

+0

是的,我改变了 'contentNegotiationManager.setIgnoreAcceptHeader(假)' 到 'contentNegotiationManager.setIgnoreAcceptHeader(真)',但我仍然得到相同的'415内容消息' – sonoerin

+0

我建议它应该是false(你的代码如上所述它是真实的) - 你希望它使用accept头并且不依赖文件扩展来检测内容类型(当设置为真正)。但这听起来像是你用两个值来试验它。 – nickdos

回答

0

两个原因:

  1. 检查是否存在循环依赖(实体之间的关联)的数据模型。如果是,请删除循环依赖项。

  2. 由于您在控制器中指定了@consumes,请确保在POST/PUT请求中提供Content-Type标头。

相关问题