我正在构建一个基于Angular / Spring MVC / Hibernate的Web应用程序。 更具体地说,我正在尝试使用表单更新数据库条目,通过从客户端向服务器发送HTTP PUT请求,但我仍然收到415错误,即使我已为内容指定了正确的标头服务器的类型,即JSON。 以下是我的代码。先感谢您。
前端http调用(来自dataService.ts)
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json'})
};
[...]
//Consulente - updateConsulente
updateConsulente(consulente: Consulente): Observable<Consulente> {
return this.http.put(this.backendUrl + "/updateConsulente", consulente, httpOptions)
.pipe(catchError(this.handleError));
}
支持映射(来自控制器java文件)
@PutMapping(value = "/updateConsulente", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Consulente> updateConsulente(@RequestBody Consulente consulente) {
consulenteService.aggiornaConsulente(consulente);
Consulente c = consulenteService.findById(consulente.getIdConsulente());
return new ResponseEntity<Consulente>(c, HttpStatus.OK);
}
我完全知道这个问题已经被问了很多,我读到这一切都试图解决这个问题,但到目前为止没有任何工作。
更新
在互联网上冲浪我明白问题可能是由于杰克逊依赖缺失错误,所以我也包括我目前的代表,如果有人可以指出一些缺失,我会非常感激。
<!-- Json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160810</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.7.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-json-jackson -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.27</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.jaxrs/jackson-jaxrs-json-provider -->
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.9.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.jaxrs/jackson-jaxrs-base -->
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-base</artifactId>
<version>2.9.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.module/jackson-module-jaxb-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jaxb-annotations</artifactId>
<version>2.9.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.ext/jersey-entity-filtering -->
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-entity-filtering</artifactId>
<version>2.27</version>
</dependency>