apache wink server stub返回null,其余客户端抛出异常

时间:2013-07-26 16:06:37

标签: rest return apache-wink

服务器存根

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

============================== 客户端存根

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

抛出异常:

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)

当服务器存根返回null时,客户端抛出此异常

我该怎么办,让客户端不抛出异常,让响应[User = null]

1 个答案:

答案 0 :(得分:0)

您需要完全按照错误描述进行操作。为MediaType.APPLICATION_OCTET_STREAM创建MessageBodyReader。

如果您的响应中没有有效负载,并且当您返回null时,客户端正在寻找为MediaType.APPLICATION_OCTET_STREAM注册的提供者,无论您在请求中设置了什么接受标头以及什么是实际标头的回应。

如果您不喜欢这种行为,可以使用Jersey客户端。