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

k5ifujac  于 2024-01-05  发布在  Spring
关注(0)|答案(3)|浏览(188)

当我尝试使用后端的API发布一个元素时,出现了一个无法解释的错误。API返回了一个错误,代码为415,与媒体类型有关:
无法加载资源:服务器以状态415()响应
我的后端返回此错误:
已解决处理程序执行导致的异常:org.springframework.web.HttpMediaTypeNotSupportedException:不支持内容类型“text/plain;charset=UTF-8”
编辑:Guru解决方案错误:
已解决处理程序执行导致的异常:org.springframework.web.HttpMediaTypeNotSupportedException:不支持内容类型“text/plain”
令人讨厌的是,我在请求中添加了以下标题:
Content-Type:application/JSON
并且正文被正确解析为JSON格式。
我使用的是Angular 5,我的后端是用Spring开发的。

Angular 客户端代码:

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

字符串
其中body方法为:

  1. body() {
  2. const object = {
  3. id: this.id,
  4. name: this.name,
  5. 'num_execs': this.num_execs
  6. };
  7. return JSON.stringify(object);
  8. }

Spring API代码:

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


其中,类的RequestMapping为/projects

  1. @RestController
  2. @RequestMapping("/projects")
  3. public class ProjectResource {
  4. @RequestMapping(value = "", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
  5. public ResponseEntity<?> addLocation(@RequestBody Project project) {
  6. return new ResponseEntity<>(esProjectService.save(project), HttpStatus.OK);
  7. }
  8. ... OTHER METHODS...
  9. }

dw1jzc5e

dw1jzc5e1#

我已经讨论过了,解决这个问题的一种方法是指定类型@RequestPart(value =“nameOfResource”and consumes = {“multipart/form-data”}
不要忘记在Angular中指定Content的名称。我希望这会有所帮助。
下面是一个例子:

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

字符串

zwghvu4y

zwghvu4y2#

在angular 5中,HttpHeaders是不可变的。

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

字符串

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


以这种方式设置头,它应该解决你的问题。我已经把示例代码只是为了解释你应该如何添加多个头。如果你不需要它,不要把'X-XSRF-TOKEN'

w51jfk4q

w51jfk4q3#

我认为错误来自于你想在发送纯文本的同时使用application / json。

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

字符串
而在前面的代码中,您发送纯文本/文本

  1. return JSON.stringify(object);


只需删除JSON.stringify调用并返回json对象,一切都很好。
变化

  1. body() {
  2. const object = {
  3. id: this.id,
  4. name: this.name,
  5. 'num_execs': this.num_execs
  6. };
  7. return JSON.stringify(object);
  8. }


  1. body() {
  2. const object = {
  3. id: this.id,
  4. name: this.name,
  5. 'num_execs': this.num_execs
  6. };
  7. return object;
  8. }

展开查看全部

相关问题