By kswaughs | Thursday, November 26, 2015

How to pass headers to REST request with Apache CXF

In my previous posts, I have explained how to call RESTful services using Apache CXF.

CXF Rest client to call POST method using WebClient

CXF Rest client to call GET method using WebClient

For the same example, we will see how to add custom headers to the REST request.

There are two ways to add the headers.

  • Example 1 - Chained method calls of header(key, value) on WebClient object
  • Example 1 - Using MultivaluedMap to pass multiple headers

Using header(key, value) method on WebClient object

Example 1
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) {

  List<Object> providers = new ArrayList<Object>();
  providers.add(new JacksonJsonProvider());

  UserRequest req = new UserRequest();
  req.setId("1234578");
  req.setName("kswaughs");

  WebClient client = WebClient.create(
          "http://127.0.0.1:8080/camel-rest-0.0.1-SNAPSHOT/user/add",
          providers);

  client = client.accept("application/json").type("application/json")
    .header("consumer_id", "StandAloneRestClient")
    .header("consumer_location", "US");

  Response r = client.post(req);
  UserResponse resp = r.readEntity(UserResponse.class);

  System.out.println(resp);
 }
}

Using MultivaluedMap to pass multiple headers

Example 2
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) {

  List<Object> providers = new ArrayList<Object>();
  providers.add(new JacksonJsonProvider());
  
  UserRequest req = new UserRequest();
  req.setId("1234578");
  req.setName("kswaughs");

  WebClient client = WebClient.create(
       "http://127.0.0.1:8080/camel-rest-0.0.1-SNAPSHOT/user/add", 
       providers);
  
  MultivaluedMap<String, String> headers = 
       new MultivaluedHashMap<String, String>();
  
  headers.add("consumer_id", "StandAloneRestClient");
  headers.add("consumer_location", "US");
  
  client = client.accept("application/json").
                  type("application/json").
                  headers(headers);
  
  Response r = client.post(req);
  UserResponse resp = r.readEntity(UserResponse.class);

  System.out.println(resp);
 }
}

In the server console, See the passed header values highlighted in bold

ID: 1
Address: http://127.0.0.1:8080/camel-rest-0.0.1-SNAPSHOT/user/add
Encoding: ISO-8859-1
Http-Method: POST
Content-Type: application/json
Headers: {Accept=[application/json], cache-control=[no-cache], connection=[keep-alive], consumer_id=[StandAloneRestClient], consumer_location=[US], Content-Length=[34], content-type=[application/json], host=[127.0.0.1:8080], pragma=[no-cache], user-agent=[Apache CXF 3.1.2]}
Payload: {"id":"1234578","name":"kswaughs"}

Recommend this on


2 comments:

  1. Hi. How can you subsequently read the custom values from the header?

    ReplyDelete
    Replies
    1. What do you mean by reading custom values ? If you are asking for reading headers from response, Follow this. http://www.kswaughs.com/2015/11/how-to-read-headers-from-rest-response.html

      Delete