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

k5ifujac  于 5个月前  发布在  Spring
关注(0)|答案(3)|浏览(60)

当我尝试使用后端的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 客户端代码:

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

dw1jzc5e

dw1jzc5e1#

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

}

字符串

zwghvu4y

zwghvu4y2#

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

w51jfk4q

w51jfk4q3#

我认为错误来自于你想在发送纯文本的同时使用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;
  }

相关问题