Using JAXB To Unmarshal a SOAP Response

This page describes how to unmarshal a SOAP response using JAXB. This is useful when you have saved a SOAP response for later consumption. The application described here uses all the JAX-WS xml to bean mapping annotations that were generated using wsimport from the service WSDL file.


Before you start please ensure you have generated all the web service client classes by using wsimport. Once this is done the generated classes will have all the annotations necessary for JAXB to use.

Use the example below as a guide for your own application.

The following is the SOAP Message we want to UN-marshal.


<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
      <ns2:getCustomerResponse xmlns:ns2="http://com/test/">

The application below open the SOAP response xml from a file called response.xml. But your application can load the data from any input stream.

The app’s main method skips to the the getCustomerResponse tag within response.xml using a while loop. Once found it loads data into the GetCustomerResponse.class generated by the wsimport tool and simply prints the information to the console.

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.stream.StreamSource;

import com.test.client.Customer;
import com.test.client.GetCustomerResponse;

public class App2 {
	private static final String FILE_NAME = "response.xml";

	public static void main(String args[]) throws Exception {

	protected static void getCustomerResponse() throws FactoryConfigurationError,
			XMLStreamException, JAXBException {
		XMLInputFactory xif = XMLInputFactory.newFactory();
		StreamSource xml = new StreamSource(FILE_NAME);
		XMLStreamReader xsr = xif.createXMLStreamReader(xml);
		xsr.nextTag(); // Advance to Envelope tag
		while (!xsr.getLocalName().equals("getCustomerResponse")) {

		JAXBContext jc = JAXBContext.newInstance(GetCustomerResponse.class);
		Unmarshaller unmarshaller = jc.createUnmarshaller();
		JAXBElement<GetCustomerResponse> je = unmarshaller.unmarshal(xsr, GetCustomerResponse.class);

		for(Customer customer : je.getValue().getCustomer()) {
			System.out.println("customerId: " + customer.getId());



The following is the WSDL file. It is not necessary for the application above to work. Its included here for reference purposes.

<definitions name='CustomerServiceImplService' targetNamespace='http://com/test/' xmlns='http://schemas.xmlsoap.org/wsdl/' xmlns:ns1='http://com/test/' xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/' xmlns:tns='http://test.com/' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
  <xs:schema targetNamespace='http://com/test/' version='1.0' xmlns:tns='http://com/test/' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
   <xs:element name='customer' type='tns:customer'/>
   <xs:element name='getCustomer' type='tns:getCustomer'/>
   <xs:element name='getCustomerResponse' type='tns:getCustomerResponse'/>
   <xs:complexType name='getCustomer'>
     <xs:element minOccurs='0' name='arg0' type='xs:string'/>
   <xs:complexType name='getCustomerResponse'>
     <xs:element maxOccurs='unbounded' minOccurs='0' name='customer' type='tns:customer'/>
   <xs:complexType name='customer'>
     <xs:element form='qualified' minOccurs='0' name='id' type='xs:string'/>
     <xs:element form='qualified' minOccurs='0' name='name' type='xs:string'/>
 <message name='CustomerService_getCustomerResponse'>
  <part element='ns1:getCustomerResponse' name='getCustomerResponse'></part>
 <message name='CustomerService_getCustomer'>
  <part element='ns1:getCustomer' name='getCustomer'></part>
 <portType name='CustomerService'>
  <operation name='getCustomer' parameterOrder='getCustomer'>
   <input message='ns1:CustomerService_getCustomer'></input>
   <output message='ns1:CustomerService_getCustomerResponse'></output>
 <binding name='CustomerServiceBinding' type='ns1:CustomerService'>
  <soap:binding style='document' transport='http://schemas.xmlsoap.org/soap/http'/>
  <operation name='getCustomer'>
   <soap:operation soapAction=''/>
    <soap:body use='literal'/>
    <soap:body use='literal'/>
<definitions name='CustomerServiceImplService' targetNamespace='http://test.com/' xmlns='http://schemas.xmlsoap.org/wsdl/' xmlns:ns1='http://com/test/' xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/' xmlns:tns='http://test.com/' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
 <import location='http://localhost:8080/jax-ws-spring-jboss/customerService?wsdl&amp;resource=CustomerService_PortType7365405514847117757.wsdl' namespace='http://com/test/'></import>
 <service name='CustomerServiceImplService'>
  <port binding='ns1:CustomerServiceBinding' name='CustomerServiceImplPort'>
   <soap:address location='http://localhost:8080/jax-ws-spring-jboss/customerService'/>

3 Responses to “Using JAXB To Unmarshal a SOAP Response”

  1. November 27, 2014 at 5:17 am

    I have created GetCustomerResponse and Customer, But The customer id value is null after excution

  2. November 27, 2014 at 5:19 am

    Thanks for the article!!

    public class GetCustomerResponse {

    List customer=new ArrayList();

    public GetCustomerResponse()

    public GetCustomerResponse(List list)

    public List getCustomer() {
    return customer;
    public void setCustomer(List customer) {
    this.customer = customer;

    public class Customer1 {

    public String id;

    public String getId() {
    return id;

    public void setId(String id) {
    this.id = id;

    It is giving CustomerId as null, Can u help me in this??

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 77 other followers

October 2013
« Aug   Dec »

Blog Stats

  • 795,599 hits

%d bloggers like this: