Add HTTP Headers To SOAP Request Using CXF

Add HTTP Headers to a SOAP Request Using CXF explains about step by step details of setting custom http headers to a SOAP Request and retrieve the headers in server side by using CXF.

Message message = PhaseInterceptorChain.getCurrentMessage();
SoapMessage soapMessage = (SoapMessage) message;
List list = soapMessage.getHeaders(); 
for (Header header : list) { 
  System.out.println("Country: "+((Element)header.getObject()).getTextContent()); 

On above code, we are getting list of header from SoapMessage


You can also use cxf interceptor in order to add HTTP Headers to a SOAP Request, see more about CXF Interceptors below.

3) CXF Interceptor Example

Required Libraries

You need to download

  1. JDK 6
  2. Eclipse 3.7
  3. CXF-2.7.3
  4. Tomcat 7

Following jar must be in classpath

  1. aopalliance-1.0.jar
  2. commons-logging-1.1.1.jar
  3. cxf-2.7.3.jar
  4. httpasyncclient-4.0-beta3.jar
  5. httpclient-4.2.1.jar
  6. httpcore-4.2.2.jar
  7. httpcore-nio-4.2.2.jar
  8. neethi-3.0.2.jar
  9. spring-aop-3.0.7.RELEASE.jar
  10. spring-asm-3.0.7.RELEASE.jar
  11. spring-beans-3.0.7.RELEASE.jar
  12. spring-context-3.0.7.RELEASE.jar
  13. spring-core-3.0.7.RELEASE.jar
  14. spring-expression-3.0.7.RELEASE.jar
  15. spring-web-3.0.7.RELEASE.jar
  16. woodstox-core-asl-4.4.0.jar
  17. stax2-api-3.1.4.jar
  18. wsdl4j-1.6.2.jar
  19. xmlschema-core-2.0.3.jar

Add Custom HTTP Headers To SOAP Request Using CXF

I am creating a sample web service project that pass Student object and return with some changes on that object. The service is using simple POJO (Plain Old Java Object) bean.

Firstly create a Dynamic Web Project (File->New->Dynamic Web Project) named "CXFTutorial" according to following screenshot

Create CXF Project CXF Tutorial

Create a Student Object

package com.student;

public class Student {
private String name;
public String getName() {
return name;
public void setName(String name) { = name;

1 2 3 Next

Your email address will not be published. Required fields are marked *