如何使用球衣发送和接收包含JSON的PUT请求? [英] How to send and receive a PUT request containing JSON with jersey?

查看:83
本文介绍了如何使用球衣发送和接收包含JSON的PUT请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我为服务器准备的东西:

Here is what I have for server:

@PUT
@Path("/put")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.TEXT_PLAIN })
public Response insertMessage(Message m) {
    return Response.ok(m.toString(), MediaType.TEXT_PLAIN).build();
}

对于客户:

ClientConfig config = new DefaultClientConfig();
config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
Client client = Client.create(config);
WebResource service = client.resource(getBaseURI());
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(new Message("a", "b", "message"));
ClientResponse response = service.path("put").accept(MediaType.APPLICATION_JSON)
                .type(MediaType.APPLICATION_JSON)
               .put(ClientResponse.class, json);
System.out.println(response.getStatus() + " " + response.getEntity(String.class));

消息:

public class Message {
    private String sender;
    private String receiver;
    private String content;
    @JsonCreator
    public Message() {}
    @JsonCreator
    public Message(@JsonProperty("sender") String sender,
            @JsonProperty("receiver")String receiver,
            @JsonProperty("content")String content) {
        this.sender = sender;
        this.receiver = receiver;
        this.content = content;
    }
}

我一直得到HTTP406.我有

And I kept getting HTTP 406. I have

<init-param>
    <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
    <param-value>true</param-value>
</init-param>

在我的web.xml中.

in my web.xml.

推荐答案

您收到406错误,因为您的Jersey资源和您的客户请求不匹配:Jersey正在生成文本响应,但您的客户表示会仅接受JSON.这是W3C关于406错误的说法:

You're getting a 406 error because your Jersey resource and your client request aren't matching up: Jersey is generating a text response but your client states it will only accept JSON. Here's what W3C says about a 406 error:

根据请求中发送的接受标头,请求所标识的资源只能生成响应实体,这些响应实体的内容特征不可接受.

The resource identified by the request is only capable of generating response entities which have content characteristics not acceptable according to the accept headers sent in the request.

您将需要更改Jersey PUT方法以生成JSON ...

You'll need to either change your Jersey PUT method to produce JSON...

...
@Produces({ MediaType.APPLICATION_JSON })
public Response insertMessage(Message m) {
    return Response.ok(m.toString()).build();
}

或在客户端请求上使用text/plain作为您接受的媒体类型:

Or use text/plain for your accept media type on the client request:

service.accept(MediaType.TEXT_PLAIN);

查看您的编辑,原始的415错误是由于客户端请求中缺少service.type(MediaType.APPLICATION_JSON)引起的.再次在W3C中,出现415错误是:

Looking over your edits, the original 415 error was caused by the missing service.type(MediaType.APPLICATION_JSON) from the client request. Again from W3C, a 415 error is:

服务器拒绝为请求提供服务,因为请求的实体的格式不受请求的方法所请求的资源支持.

The server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method.

这是我正在使用的W3C参考: http://www.w3 .org/Protocols/rfc2616/rfc2616-sec10.html

Here's the W3C reference I'm using: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

这篇关于如何使用球衣发送和接收包含JSON的PUT请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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