【问题标题】:Unsupported Media Type with Content-Type header?带有 Content-Type 标头的不受支持的媒体类型?
【发布时间】:2018-07-09 04:58:17
【问题描述】:

当我尝试使用后端的 API 发布元素时,我遇到了莫名其妙的错误。 API 返回代码为 415 的错误,与媒体类型相关:

Failed to load resource: the server responded with a status of 415 ()

我的后端返回此错误:

Resolved exception caused by Handler execution: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'text/plain;charset=UTF-8' not supported

编辑:Guru 解决方案出错:

Resolved exception caused by Handler execution: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'text/plain' not supported

烦人的是我在请求中添加了这个标头:

Content-Type: application/json

并且正文被正确解析为 JSON 格式。

我用的是Angular 5,我的后端是用Spring开发的。

Angular 客户端代码:

postProject(project: Project) {
    const headers: HttpHeaders = new HttpHeaders();
    headers.append('Content-Type', 'application/json');
    return this.http.post('http://localhost:8443/projects', project.body(), {headers: headers})
      .map(response => response);
  }

body 方法在哪里:

body() {
    const object = {
      id: this.id,
      name: this.name,
      'num_execs': this.num_execs
    };
    return JSON.stringify(object);
  }

Spring API 代码:

@RequestMapping(value = "", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> addLocation(@RequestBody Project project) {
        return new ResponseEntity<>(esProjectService.save(project), HttpStatus.OK);
    }

类的RequestMapping是/projects:

    @RestController
    @RequestMapping("/projects")
    public class ProjectResource {

       @RequestMapping(value = "", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
       public ResponseEntity<?> addLocation(@RequestBody Project project) {
           return new ResponseEntity<>(esProjectService.save(project), HttpStatus.OK);
       }

       ... OTHER METHODS...
    }

【问题讨论】:

  • 您是否尝试将consumes 属性添加到您的spring 方法RequestMapping 中?如@RequestMapping(value = "/methodName", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
  • @GuruprasadRao 您的解决方案部分解决了我的错误。现在,输出错误在我编辑的帖子中。
  • 能否请您添加有关您的代码的更多详细信息?您的服务器方法以及您在网络控制台中发送客户端请求 + 请求数据的方式..
  • @GuruprasadRao 代码已添加。
  • 为什么value = "" 在请求映射中?它将如何映射到/projects

标签: spring angular api header content-type


【解决方案1】:

我已经经历过了,解决它的一种方法是指定类型 @RequestPart (value = "nameOfResource" and consumes = {"multipart/form-data"}

不要忘记在 Angular 中指定 Content 的名称。 希望对你有帮助。

下面是一个例子:

RequestMapping(value = "/add", method = RequestMethod.POST, consumes = {"multipart/form-data"}, produces = "application/json")
        public ResponseEntity<?> add(@RequestPart(value = "image", required = true) MultipartFile image, 
                                     @RequestPart(value = "team", required = true) @Valid Team team, BindingResult bResult) {

}

【讨论】:

    【解决方案2】:

    在 Angular 5 中,HttpHeaders 是不可变的。因此,你应该这样使用它

    let headers = new HttpHeaders({ 
          'Content-Type': 'application/json',
          'X-XSRF-TOKEN': token
        });
    

    let headers = new HttpHeaders();
    headers = headers.append('Content-Type', 'application/json');
    headers = headers.append('X-XSRF-TOKEN', token);
    

    以这种方式设置标题,它应该可以解决您的问题。我放了示例代码只是为了解释你应该如何添加多个标题。如果您不需要,请不要输入'X-XSRF-TOKEN'

    【讨论】:

      【解决方案3】:

      我认为错误来自这样一个事实,即您想在发送纯文本/文本时使用应用程序/json。 我解释一下,在你的服务中你指定

      @RequestMapping(value = "", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
      

      在您的前端代码中,您发送纯文本/文本

       return JSON.stringify(object);
      

      只需删除 JSON.stringify 调用并返回 json 对象就可以了。

      改变

      body() {
          const object = {
            id: this.id,
            name: this.name,
            'num_execs': this.num_execs
          };
          return JSON.stringify(object);
        }
      

      body() {
          const object = {
            id: this.id,
            name: this.name,
            'num_execs': this.num_execs
          };
          return object;
        }
      

      【讨论】:

        猜你喜欢
        • 2021-12-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-26
        • 2019-04-30
        • 2021-04-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多