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

IT小君   2022-09-15T08:32:31

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

编辑:大师解决方案出错:

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

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

Content-Type: application/json

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

我正在使用Angular 5,我的后端是使用Spring.

角客户端代码:

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);
  }

春天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)
IT小君

我已经经历过这个,解决它的一种方法是指定类型 @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) {

}
2022-09-15T08:32:31   回复
IT小君

在角度 5HttpHeaders中是不可变的。因此,您应该像这样使用它

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'

2022-09-15T08:32:31   回复
IT小君

我认为错误来自这样一个事实,即您想在发送纯文本/文本时使用应用程序/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;
  }
2022-09-15T08:32:31   回复