如何为cxf jax-rs 2.0客户端注册jackson json提供程序? [英] How to register jackson json provider for cxf jax-rs 2.0 client?

查看:210
本文介绍了如何为cxf jax-rs 2.0客户端注册jackson json提供程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在发出简单GET请求的JAX-RS客户端。我正在使用CXF实现和Spring for DI。呼叫成功,我得到200响应代码。但是当我在POJO中阅读回复时出现错误。

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.

例外:

[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]

代码:

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 answer in my classpath, it doesn't seem to be picked up automatically.

    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
    </dependency>

我还尝试在创建客户端时注册json提供程序:

I also tried registering the json provider when creating the client:

Client client = ClientBuilder.newClient().register(new JacsksonJsonProvider());

Client client = ClientBuilder.newClient().register(JacsksonJsonProvider.class);

但这些选项都没有奏效。当我使用上述选项之一注册json提供程序时,我得到了一个不同的异常:

But none of these options worked too. I got a different exception when I registered the json provider using one of the options above:

javax.ws.rs.client.ResponseProcessingException: Problem with reading the data

更新:

使用 ClientBuilder.newClient()。register(JacsksonJsonProvider.class)注册json提供程序工作正常。问题在于数据(就像上面的例外清楚地说明了......我现在觉得很傻:()。我在json中有一个名为active的布尔字段,但它在POJO中被称为isActive。一旦我添加了注释 @JsonProperty(active)到POJO中的字段,它开始正常工作

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

推荐答案

AFAIK CXF MessageBodyReader 类的noreferrer>不支持自动发现。但是手动注册 JacksonJsonProvider 应该适合你。

AFAIK CXF does not support autodiscovery of MessageBodyReader classes. But registering manually JacksonJsonProvider should work for you.

请检查我的例子,效果非常好。它与你的几乎完全一样,我只是使用了不同的服务。也许你可以发现阻止你的v的差异正常工作。

Please check my example that 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(JacksonJsonProvider.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);

这篇关于如何为cxf jax-rs 2.0客户端注册jackson json提供程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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