How to send and receive PUT request containing jersey JSON?

Here's what I have for the 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();
}

      

for client:

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));

      

For the message:

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

      

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>

      

in my web.xml.

+3


source to share


2 answers


You are getting a 406 error because your Jersey resource and your client request do not match: Jersey generates a text response, but your client states that it will only accept JSON. Here's what the W3C says about the 406 error:

The resource identified by the request is capable of generating response objects that do not have content characteristics that are unacceptable according to the accept headers sent in the request.

You will either need to change the Jersey PUT method to generate the JSON ...

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

      

Or use text/plain

for your media type of reception in the client request:



service.accept(MediaType.TEXT_PLAIN);

      

Looking back on your changes, the original 415 error was caused by an absence service.type(MediaType.APPLICATION_JSON)

from a client request. Again from W3C error 415:

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

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

+1


source


Received HTTP response code

415 Unsupported Media Type

      

Have you tried setting accept property on WebResource? Something like that:



service.accept(MediaType.APPLICATION_JSON);

      

Take a look at this thread . Seems to be the same problem.

0


source







All Articles