简体   繁体   English

如何解决415不支持的媒体类型错误?

[英]How to resolve 415 unsupported media type error?

I have angular 5 which try to send PUT request to a rest service in java application: 我有角度5尝试将PUT请求发送到Java应用程序中的rest服务:

The Angular call: Angular调用:

    ...
    import 'rxjs/add/operator/toPromise';

    const httpOptions = {
        headers: new HttpHeaders({ 'Accept': 'application/json', 'Content-Type': 'application/json' })
    };

    @Injectable()
    export class TripService {

...

    saveTrip(trip: Trip): Observable<any> {
    return this.http.put(`${this.baseUrl}/Trip`, JSON.stringify(trip), httpOptions);
  }

The server (java) service: 服务器(java)服务:

@PUT
@Consumes("application/json")
public Response updateTrip(Trip trip) {
    tripDao.updateTrip(trip);
    return Response.ok().entity("trip updated successfully").build();
}

The entity!: 实体!:

@Entity
@Table(name = "trip")
@XmlRootElement
public class Trip implements java.io.Serializable {

    private static final long serialVersionUID = 1L;

    @XmlElement(name = "id")
    private Integer id;
    @XmlElement(name = "user")
    private User user;
    @XmlElement(name = "start_date")
    private Date startDate;
    ...

Request header: 请求标头:

Accept: application/json
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9,he;q=0.8
Authorization: Bearer fake-jwt-token
Connection: keep-alive
Content-Length: 538
Content-Type: application/json
Host: localhost:8080
Origin: http://localhost:4200
Referer: http://localhost:4200/manager/company
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36 

The error: 错误:

Request URL: http://localhost:8080/... /rest/Trip
Request Method: PUT
Status Code: 415 Unsupported Media Type
Remote Address: [::1]:8080
Referrer Policy: no-referrer-when-downgrade

I need help with the JSON to Java conversion ? 我需要将JSON转换为Java的帮助吗? I have this jar for example: javax.ws.rs-api-2.0.1 我有这个罐子,例如:javax.ws.rs-api-2.0.1

I added the jackson jars which included currently in the project: 我添加了当前项目中包含的杰克逊罐子: 杰克逊罐

Your client is requesting application/json in its Accept header, but the server does not produce it. 您的客户端在其Accept标头中请求application/json ,但服务器未生成它。 Add the Produces annotation that declares that the content type is supported. 添加Produces批注,声明该内容类型受支持。

@PUT
@Consumes("application/json")
@Produces("application/json")
public Response updateTrip(Trip trip) {

Beside this, your server is not responding with JSON , which is a different problem altogether. 除此之外, 您的服务器没有响应JSON ,这完全是另一个问题。 You probably want to annotate the method with: 您可能想用以下方法注释该方法:

@Consumes("text/plain") 

The most likely fix you need is to make your client send Accept: */* , or Accept: text/plain 您最可能需要的解决方案是使客户端发送Accept: */*Accept: text/plain

Use Jackson 使用杰克逊

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.5</version>
    </dependency>

Unless you have any specific case, it can convert your XML response to JSON without any additional code. 除非有任何特殊情况,否则它可以将XML响应转换为JSON,而无需任何其他代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM