带有Content-Type标头的不支持的媒体类型?

时间:2018-01-30 10:29:22

标签: spring angular api header content-type

当我尝试使用后端的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...
    }

3 个答案:

答案 0 :(得分:0)

我已经完成了这个,解决它的一种方法是指定类型@RequestPart(value =“nameOfResource”和consumemes = {“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) {

}

答案 1 :(得分:0)

在角度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'

答案 2 :(得分:0)

我认为错误来自于您在发送普通/文本时要使用application / 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;
  }