带有Content-Type标头的不受支持的媒体类型? [英] Unsupported Media Type with Content-Type header?

查看:882
本文介绍了带有Content-Type标头的不受支持的媒体类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用后端的API发布元素时,出现了莫名其妙的错误.该API返回错误代码415,与媒体类型有关:

I'm getting an inexplicable error when I try to post an element by using the API of my backend. The API returns an error with code 415, related to Media Type:

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

我的后端向我返回此错误:

My backend returns me this error:

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

Guru解决方案出错:

Error with Guru solution:

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

烦人的是,我已将此标头添加到我的请求中:

The annoying thing is that I have added this header to my request:

Content-Type: application/json

并且主体已正确解析为JSON格式.

And the body is correctly parsed into JSON format.

我正在使用Angular 5,并且我的后端是通过使用Spring开发的.

I am using Angular 5, and my backend has been developed by using 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);
  }

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的地方:

Where RequestMapping of the class is /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...
    }

推荐答案

我认为错误是由于您希望在发送纯文本/文本时消耗application/json. 我解释说,在您指定的服务中,

I think the mistake comes from the fact that you want to consume application / json while you send plain / text. I explain, in your service you specify

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

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

while in your front code you send plain / text

 return JSON.stringify(object);

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

Just delete the JSON.stringify call and return the json object and all is well.

更改

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

这篇关于带有Content-Type标头的不受支持的媒体类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆