【问题标题】:Unable to get collection response from cxf rest web service无法从 cxf REST Web 服务获取收集响应
【发布时间】:2014-03-27 08:10:48
【问题描述】:

我正在尝试获取 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);
        }

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

【问题讨论】:

    标签: web-services rest cxf cxfrs


    【解决方案1】:

    在您的 REST 客户端中创建 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,您还需要提供以下所需的 jar 来解决项目中的 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> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-14
      • 1970-01-01
      • 2012-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多