0

Server stub

  @GET
    @Path("/user/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public User getUser(@PathParam("id") String id){
        User user =  myService.getUserById(id);
        if (user!= null){
            return user;
        }
        return null;
    }

============================== Client stub

User response = WinkRestClient.resource(path).contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).get(User.class);

throw exception:

Exception in thread "main" java.lang.RuntimeException: A javax.ws.rs.ext.MessageBodyReader implementation was not found for class com.test.entity.User type and application/octet-stream media type.  Verify that all entity providers are correctly registered.  Add a custom javax.ws.rs.ext.MessageBodyReader provider to handle the type and media type if a JAX-RS entity provider does not currently exist.
    at org.apache.wink.client.internal.handlers.ClientResponseImpl.readEntity(ClientResponseImpl.java:122)
    at org.apache.wink.client.internal.handlers.ClientResponseImpl.getEntity(ClientResponseImpl.java:65)
    at org.apache.wink.client.internal.handlers.ClientResponseImpl.getEntity(ClientResponseImpl.java:52)
    at org.apache.wink.client.internal.ResourceImpl.invoke(ResourceImpl.java:196)
    at org.apache.wink.client.internal.ResourceImpl.get(ResourceImpl.java:303)
    at com.wolianw.sale.bl.ClResourceTest.main(ClResourceTest.java:14)

when server stub return null ,the client throw this exception

what can I do,let the client don't throw exception,let response [User = null]

1 Answers1

0

You need to do exactly what is described in error. Create MessageBodyReader for MediaType.APPLICATION_OCTET_STREAM.

If you have no payload in your response and that is the case when you return null, the client is looking for provider registered for MediaType.APPLICATION_OCTET_STREAM, no matter what accept header you set in your request and what is actual header of the response.

You may use the Jersey client if you don't like this behavior, I do.

swist
  • 1,111
  • 8
  • 18