【问题标题】:Spring mvc 4 @PostMapping or @RequestMapping expose rest give 415 responseSpring mvc 4 @PostMapping 或 @RequestMapping 暴露休息给出 415 响应
【发布时间】:2019-03-10 13:10:31
【问题描述】:

我正在尝试使用 spring mvc 4 公开 rest webservice。 在控制器中使用这些注释

  @RequestMapping(value = "/services/empservice", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
  public String testArgument(@RequestBody Employee employee){
      System.out.println("employee variable--->"+employee.getEmployeeID());
      return String.valueOf(20);
   }

  @PostMapping(value = "/services/empservices", produces = MediaType.APPLICATION_JSON_VALUE,headers = "content-type=application/x-www-form-urlencoded")
  public String testArguments(@RequestBody Employee emploee){
      System.out.println("employee variable--->"+employee.getEmployeeID());
      return employee.getEmployeeID();
   }

WAS 8.5.5 的响应给出 415 Media unsupported Status with 响应头 接受 →application/octet-stream, text/plain, application/xml, text/xml, application/x-www-form-urlencoded, application/*+xml, multipart/form-data, /


接受 →application/x-www-form-urlencoded


如何通过此错误以及如何设置响应标头以获取带有 json 响应的 application/json

【问题讨论】:

  • remove header headers = "content-type=application/x-www-form-urlencoded"
  • 在您的请求中,您需要设置一个标头属性 Content-Type = application/json
  • 删除现有的 headers = "content-type=application/x-www-form-urlencoded" 后是一样的 415
  • 通过添加 headers = "content-type=application/json" 也没有帮助,这是同样的错误

标签: spring spring-mvc


【解决方案1】:

您的问题应该在下一期的Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported for @RequestBody 答案中得到解决。

必要时删除注释@RequestBody

【讨论】:

    猜你喜欢
    • 2011-09-21
    • 2011-10-11
    • 1970-01-01
    • 2020-02-20
    • 2015-11-18
    • 2017-10-10
    • 1970-01-01
    • 2016-08-01
    • 1970-01-01
    相关资源
    最近更新 更多