Reputation: 5786
I'm writing a JSX-RS based Spring+CXF client for a service that sends a simple response as below.
JSON : Response
{
"message": "Hey Karthik"
}
I have the following configuration in my spring.xml:
<jaxrs:providers>
<bean class="org.apache.cxf.jaxrs.provider.json.JSONProvider">
<property name="dropRootElement" value="true" />
</bean>
</jaxrs:providers>
My entity class looks like this
@XmlRootElement
public class HiModel {
private String message;
public HiModel(){}
.
.
.
}
My JAX-RS client is like this:
@Test
public void getMessage(){
WebClient client = WebClient.create("http://localhost:8182");
client.path("hiService/sayHi/hi");
client.type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON);
Response r = client.get();
System.out.println(r.readEntity(HiModel.class));
}
I get the error : No message body reader has been found for class com.karthik.model.HiModel, ContentType: application/json
How do I resolve it? There are lots of questions with the method I chose to write as client, but I first need to get this resolved. Please help.
EDIT 1 : I can resolve it by
System.out.println(r.readEntity(String.class));
But, How do I resolve it with the entity as HiModel.class
Upvotes: 2
Views: 22210
Reputation: 1445
Add it to the webclient object.
List<Object> providers = new ArrayList<>();
// add custom providers if any
providers.add(new JacksonJaxbJsonProvider());
WebClient client = WebClient.create(ENDPOINT_ADDRESS,providers);
If you are not using spring to configure cxf then:
1) in Web.xml
<servlet>
<display-name>CXFNonSpringJaxrsServlet</display-name>
<servlet-name>CXFNonSpringJaxrsServlet</servlet-name>
<servlet-class>org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet
</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>
com.jaxrs.JaxRsConfigApplication
</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>CXFNonSpringJaxrsServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
2) On the JaxRsConfigApplication.java
public class JaxRsConfigApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new HashSet<>();
resources.add(ServicioPistaRest.class);
resources.add(ConsultarDatosSolicitud.class);
return resources;
}
@Override
public Set<Object> getSingletons() {
Set<Object> classes = new HashSet<>();
JacksonJaxbJsonProvider jacksonJaxbJsonProvider = new JacksonJaxbJsonProvider();
classes.add(jacksonJaxbJsonProvider);
return classes;
}
}
Upvotes: 2
Reputation: 6403
In case it helps someone. I had similar scenario, and I managed to solve it using Jackson JSON library. Using your example:
WebClient client = WebClient.create("http://localhost:8182/hiService/sayHi/hi");
Response r = client.accept("application/json").get();
MappingJsonFactory factory = new MappingJsonFactory();
JsonParser parser = factory.createJsonParser((InputStream)r.getEntity());
HiModel hiModel= parser.readValueAs(HiModel.class);
Very similar test is actually present within Apache CXF JAX-RS archetype.
Upvotes: 2