java 如何为 cxf jax-rs 2.0 客户端注册 jackson json 提供程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30132336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to register Hymanson json provider for cxf jax-rs 2.0 client?
提问by Anand Jayabalan
I have a JAX-RS client that's making a simple GET request. I'm using the CXF implementation and Spring for DI. The call is successfull and I get a response code of 200 back. But I'm getting an error when reading the response into my POJO.
我有一个 JAX-RS 客户端,它正在发出一个简单的 GET 请求。我正在使用 CXF 实现和 Spring for DI。呼叫成功,我收到了 200 的响应代码。但是在将响应读入我的 POJO 时出现错误。
Exception:
例外:
[2015-05-08 16:11:55,457][ERROR][org.apache.cxf.jaxrs.utils.JAXRSUtils]: No message body reader has been found for class com.voya.refapp.domain.Customer, ContentType: application/json
[2015-05-08 16:11:55,468][ERROR][com.voya.refapp.service.CustomerServiceImpl]: filterByName() - Exception occurred
javax.ws.rs.client.ResponseProcessingException: No message body reader has been found for class com.voya.refapp.domain.Customer, ContentType: application/json
at org.apache.cxf.jaxrs.impl.ResponseImpl.reportMessageHandlerProblem(ResponseImpl.java:433) ~[cxf-rt-frontend-jaxrs-3.0.4.jar:3.0.4]
at org.apache.cxf.jaxrs.impl.ResponseImpl.doReadEntity(ResponseImpl.java:384) ~[cxf-rt-frontend-jaxrs-3.0.4.jar:3.0.4]
Code:
代码:
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080/rest").path("customers/1");
Invocation.Builder builder = target.request(MediaType.APPLICATION_JSON_TYPE);
Response response = builder.get(); // Successful
Customer customer = response.readEntity(Customer.class); // Fails
I have the below dependency as suggested in this answerin my classpath, it doesn't seem to be picked up automatically.
我在我的类路径中的这个答案中建议了以下依赖项,它似乎没有被自动拾取。
<dependency>
<groupId>com.fasterxml.Hymanson.jaxrs</groupId>
<artifactId>Hymanson-jaxrs-json-provider</artifactId>
</dependency>
I also tried registering the json provider when creating the client:
我还尝试在创建客户端时注册 json 提供程序:
Client client = ClientBuilder.newClient().register(new JacsksonJsonProvider());
and
和
Client client = ClientBuilder.newClient().register(JacsksonJsonProvider.class);
But none of these options worked too. I got a different exception when I registered the json provider using one of the options above:
但是这些选项都没有奏效。当我使用上述选项之一注册 json 提供程序时,我遇到了不同的异常:
javax.ws.rs.client.ResponseProcessingException: Problem with reading the data
Update:
更新:
Registering the json provider worked fine using ClientBuilder.newClient().register(JacsksonJsonProvider.class)
. The issue was with the data (like the exception above clearly states.. I feel silly now :(). I had boolean field in the json named "active", but it was called "isActive" in the POJO. Once I added the annotation @JsonProperty("active")
to the field in POJO, it started working fine
注册 json 提供程序使用ClientBuilder.newClient().register(JacsksonJsonProvider.class)
. 问题出在数据上(就像上面的异常清楚地说明..我现在觉得很傻:()。我在 json 中有一个名为“active”的布尔字段,但它在 POJO 中被称为“isActive”。一旦我添加了@JsonProperty("active")
POJO 字段的注释,它开始正常工作
回答by Dawid Pytel
AFAIK CXF does not support autodiscoveryof MessageBodyReader
classes. But registering manually HymansonJsonProvider
should work for you.
AFAIK CXF并没有支持自动发现的MessageBodyReader
类。但是手动注册HymansonJsonProvider
应该对你有用。
Please check my examplethat works perfectly well. It is almost exactly the same as yours, I just used different service. Maybe you can spot a difference that prevents your version from working correctly.
请检查我的示例,该示例非常有效。它和你的几乎完全一样,我只是使用了不同的服务。也许您可以发现阻止您的版本正常工作的差异。
Client client = ClientBuilder.newClient().register(HymansonJsonProvider.class);
WebTarget target = client.target("http://jsonplaceholder.typicode.com").path("posts/1");
Invocation.Builder builder = target.request(MediaType.APPLICATION_JSON_TYPE);
Response response = builder.get(); // Successful
Post post = response.readEntity(Post.class);