简体   繁体   English

不支持的媒体类型 - HTTP状态415

[英]Unsupported Media Type - HTTP Status 415

I have a problem with my web service. 我的网络服务有问题。 GET requests are executed well and correct but the post request is getting the HTTP Status 415. GET请求执行得很好并且正确,但是post请求正在获取HTTP状态415。

The project I am working on is a JAX-RS RESTful API that will need to communicate with an Android mobile application. 我正在开发的项目是一个需要与Android移动应用程序通信的JAX-RS RESTful API。 I can receive the information from the GET statement. 我可以收到GET声明中的信息。

This is the code of my LoginFormat object: 这是我的LoginFormat对象的代码:

@XmlRootElement
public class LoginFormat {

    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

Here you can see the POST sample of my CoCreationService class: 在这里,您可以看到我的CoCreationService类的POST示例:

@Path("/User")
public class CoCreationService {
    @POST
    @Path("/testLogin") 
    @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
    public Response parseTerm(LoginFormat login) {  

        return Response.status(200).entity(login.getUsername() + login.getPassword()).build();  
    }
}

I tried so much that I am confused from it. 我试了太多,以至于我很困惑。

I was testing the web service wit curl: 我正在测试web服务的卷曲:

curl -i -X POST -H 'Content-Type: application/json' -d '{"username": "testuser", "password": "test"}' http://localhost:8080/CoCreationService/api/User/testLogin

Is there some setting that needs to be said or did I make a crucial mistake? 是否有一些需要说明的设置或者我犯了一个关键错误?

PS: I am working with NetBeans. PS:我正在使用NetBeans。

Edit: A POST with text/plain works! 编辑:带文本/普通作品的PO​​ST!

@POST
@Path("/testPost")
@Consumes("text/plain")
public Response postClichedMessage(String message) {       
    return Response.status(200).entity(message).build();
}

There is nothing wrong with the code. 代码没有任何问题。 The only thing you need to do is to include proper library in your classpath which knows how to unmarshall json string. 您唯一需要做的就是在类路径中包含适当的库,它知道如何解组json字符串。 If using Maven you can simply add another dependency: 如果使用Maven,您只需添加另一个依赖项:

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>${jersey.version}</version>
</dependency>

As long as you are using Netbeans you can see in the AS log something like this: 只要您使用Netbeans,就可以在AS日志中看到如下内容:

SEVERE: A message body reader for Java class LoginFormat, and 
Java type class LoginFormat, and MIME media type application/json 
was not found.

The registered message body readers compatible with the MIME media type are:

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

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