1

我正在尝试获取 java.util.List 作为来自 cxf REST Web 服务的响应。我尝试过使用 WebClient 类的方法 postObjectGetCollection 方法,但没有运气。我得到 - org.apache.cxf.jaxrs.client.ClientWebApplicationException: .No message body reader has found for class: interface java.util.Collection, ContentType: application/json。

以下是我的客户代码-

String mediaType = "application/json";
        if (url != null) {
            List<DataTypeDTO>  resultdtos = new ArrayList<DataTypeDTO>();
            WebClient client = WebClient.create(url);
            client = client.accept(mediaType).type(mediaType).path(uri);
            resultdtos = (List<DataTypeDTO>)client.getCollection(DataTypeDTO.class); 
            System.out.println(resultdtos);
        }

如果我缺少任何配置或其他内容,请帮助我。

4

1 回答 1

1

在您的休息客户端中创建 webClient 对象时,您需要提供提供者列表。您可以使用以下代码来解决您的问题:

 final String url = "http://localhost:10227/someService";
        final String uri = "/manageXyz/fetchAllDataTypes";            
        final String mediaType = "application/json";
        Object response = null;
        List<Object> providers = new ArrayList<Object>();
        providers.add( new JacksonJaxbJsonProvider() );
        WebClient client = WebClient.create(url, providers);
        client = client.accept(mediaType).type(mediaType).path(uri);
        response = (List<Object>)client.post(oemUser, List.class); 

如果您使用的是 maven,您还需要提供以下所需的 jars 来解决项目中的 maven 依赖关系:

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-jaxrs</artifactId>
    <version>1.5.4</version>
</dependency>
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.9.13</version>
</dependency>
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-xc</artifactId>
    <version>1.9.13</version>
</dependency> 
于 2014-03-28T10:10:16.930 回答