smuco
smuco

Reputation: 117

jax-rs Response entity class

I'm trying to create a jax-rs client which posts an xml as object and receives an xml on the response body from the server. The code is as below:

import org.apache.cxf.jaxrs.client.WebClient;
..
TravelRequest tr = ...
..
WebClient client = WebClient.create(url);
client.type(javax.ws.rs.core.MediaType.APPLICATION_XML_TYPE).accept(javax.ws.rs.core.MediaType.APPLICATION_XML_TYPE);
javax.ws.rs.core.Response r = client.post(tr);
Object response = r.getEntity();

The java type of the response object is sun.net.www.protocol.http.HttpURLConnection$HttpInputStream Is it possible to get an object of TravelRequest type instead of reading the xml from input stream? Someone knows any example of it? I can also use spring to configure my client. Any help would be appreciated.

Upvotes: 1

Views: 4993

Answers (2)

Bee
Bee

Reputation: 12513

This is how it is done.

TravelRequest travelRequest = client.post(tr, TravelRequest.class);

Hope this will help someone.

Upvotes: 1

user647772
user647772

Reputation:

You are using the WebClient the wrong way. Methods like accept and type dont' alter the WebClient but return the updated Client

So the correct usage is:

WebClient client = WebClient.create(url);
Response response = client.type(...).accept(...).post(tr);

The Response.getEntity() can then be used to extract the response.

CXF supports various forms of data binding that you can use to map the response body to your classes.

Upvotes: 1

Related Questions