In my previous post, I have explained how to pass headers to RESTful services using Apache CXF.
CXF Rest client passing headers to request
For the same example, we will see how to read/parse headers from the REST response.
There are two ways to read/parse the headers from REST response
- Example 1 - Using getHeaderString method of Response object to read a single header value
- Example 1 - Using MultivaluedMap to read all header values
Using getHeaderString method of Response
package com.example.cxfrs; import java.util.ArrayList; import java.util.List; import javax.ws.rs.core.Response; import com.example.cxfrs.beans.UserRequest; import com.example.cxfrs.beans.UserResponse; import org.apache.cxf.jaxrs.client.WebClient; import org.codehaus.jackson.jaxrs.JacksonJsonProvider; public class CXFRestPostClient { public static void main(String[] args) { // Adding JacksonJsonProvider to convert JSON String to Java object List<Object> providers = new ArrayList<Object>(); providers.add(new JacksonJsonProvider()); // Build REST request UserRequest req = new UserRequest(); req.setId("1234578"); req.setName("kswaughs"); // Create WebClient WebClient client = WebClient.create( "http://127.0.0.1:8080/camel-rest-0.0.1-SNAPSHOT/user/add", providers); // Adding headers client = client.accept("application/json").type("application/json") .header("consumer_id", "StandAloneRestClient") .header("consumer_location", "US"); // Call the REST service Response r = client.post(req); //Read/parse the response headers System.out.println("date:"+ r.getHeaderString("Date")); // Read/parse the response UserResponse resp = r.readEntity(UserResponse.class); System.out.println(resp); }
Server log - Response
ID: 5 Response-Code: 200 Content-Type: application/json Headers: {Content-Type=[application/json], Date=[Fri, 27 Nov 2015 06:50:34 GMT]} Payload: {"id":"1234578","status":"Success"}
Output is
date:Fri, 27 Nov 2015 06:50:34 GMT UserResponse [status=Success, id=1234578]
Using MultivaluedMap to read all header values
package com.example.cxfrs; import java.util.ArrayList; import java.util.List; import javax.ws.rs.core.MultivaluedHashMap; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; import com.example.cxfrs.beans.UserRequest; import com.example.cxfrs.beans.UserResponse; import org.apache.cxf.jaxrs.client.WebClient; import org.codehaus.jackson.jaxrs.JacksonJsonProvider; public class CXFRestPostClient { public static void main(String[] args) { // Adding JacksonJsonProvider to convert JSON String to Java object List<Object> providers = new ArrayList<Object>(); providers.add(new JacksonJsonProvider()); // Build REST request UserRequest req = new UserRequest(); req.setId("1234578"); req.setName("kswaughs"); // Create WebClient WebClient client = WebClient.create( "http://127.0.0.1:8080/camel-rest-0.0.1-SNAPSHOT/user/add", providers); // Adding headers MultivaluedMap<String, String> reqHeaders = new MultivaluedHashMap<String, String>(); reqHeaders.add("consumer_id", "StandAloneRestClient"); reqHeaders.add("consumer_location", "US"); client = client.accept("application/json") .type("application/json") .headers(reqHeaders); // Call the REST service Response r = client.post(req); //Read/parse the response headers MultivaluedMap<String, String> respHeaders = r.getStringHeaders(); System.out.println("respHeaders:\n" + respHeaders); // Read/parse the response UserResponse resp = r.readEntity(UserResponse.class); System.out.println(resp); } }
Output is
respHeaders: {content-type=[application/json], Date=[Fri, 27 Nov 2015 07:13:16 GMT], Server=[Apache-Coyote/1.1], transfer-encoding=[chunked], Content-Type=[application/json]} UserResponse [status=Success, id=1234578]