【问题标题】:SpringMVC JSON unsupported typeSpringMVC JSON 不支持的类型
【发布时间】:2014-02-09 19:46:04
【问题描述】:

我有一个 Spring MVC 3.2 应用程序不接受来自浏览器的 JSON POST 请求。如果我使用 CocoaRest 之类的工具,一切都很好。但是当我使用像 chrome://restclient/content/restclient.html 这样的浏览器或工具时,我会得到 ​​p>

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 显示请求的 contentType 为 application/json。

如果有人知道可能出了什么问题,我当然会感谢您的帮助。

【问题讨论】:

  • 你试过设置吗(在conf文件中):contentNegotiationManager.setIgnoreAcceptHeader( false );
  • 是的,我将 'contentNegotiationManager.setIgnoreAcceptHeader( false )' 更改为 'contentNegotiationManager.setIgnoreAcceptHeader( true )' 但我仍然收到相同的 '415 内容消息'
  • 我建议它应该为假(您上面的代码将其设为真)-您希望它使用接受标头而不依赖文件扩展名来检测内容类型(有效时设置为真)。但听起来你尝试了这两个值。
  • 如果有帮助,我会更新我的帖子。

标签: json spring-mvc


【解决方案1】:

两个原因:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-10
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    • 2022-12-22
    • 2017-02-23
    • 2015-07-12
    • 2015-06-21
    相关资源
    最近更新 更多