Search code examples
web-servicesrestcxfcxfrs

Unable to get collection response from cxf rest web service


I am trying to get java.util.List as response from cxf rest web service. I have tried with WebClient class's method postObjectGetCollection method but no luck. I am getting - org.apache.cxf.jaxrs.client.ClientWebApplicationException: .No message body reader has been found for class : interface java.util.Collection, ContentType : application/json.

Below are my client code-

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

Please help me out if i am missing any configuration or other things.


Solution

  • You need to provide the provider list while creating the webClient object in your rest client. You can use the below code to resolve your issue:

     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); 
    

    If you are using maven, you also need to provide below required jars to resolve maven dependency in your project:

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