Saturday, 8 February 2014

Create Simple Soap Messages with JAXB

I want to create a SOAP Message with Java. I will use JAXB to construct the SOAP message.



  ...
  ...



  ...
  ...
 


We'll start with creating the soap envelope. First we will create two packages, tutorial, and tutorial.soap. tutorial will contain the main file. tutorial.soap will contain the JAXB files that will be used to construct the SOAP messages. We will start by creating a skeleton for the SOAP Envelope. tutorial.soap.SoapEnvelope.java
package tutorial.soap;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="envelope")
public class SoapEnvelope {

}
We'll create the class which creates the SoapEnvelope using JAXB and converts it to a string. tutorial.Test.java
package tutorial;

import java.io.StringWriter;

import tutorial.soap.SoapEnvelope;

public class Test {

 public static void main(String[] args) {
  SoapEnvelope envelope = new SoapEnvelope();
  
  try {            
            javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(envelope.getClass().getPackage().getName());
            javax.xml.bind.Marshaller marshaller = jaxbCtx.createMarshaller();
            marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_ENCODING, "UTF-8"); 
            marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            
            StringWriter writer = new StringWriter();
            
            marshaller.marshal(envelope, writer);
            
            System.out.println(writer.toString());
  }
  catch(Exception e) {
   e.printStackTrace();
  }
 }
}
This creates the following SOAP Message



Now we will create the namespace, xmlns:SOAP-ENV="http://www.w3.org/2001/12/soap-envelope". To do this, create a new Java file in the tutorial.soap package called package-info.java
@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.w3.org/2001/12/soap-envelope", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED,
        xmlns={@XmlNs(prefix="soap-env", namespaceURI="http://www.w3.org/2001/12/soap-envelope")})

package tutorial.soap;

import javax.xml.bind.annotation.XmlNs;
This will create the following XML


Next we will create the SOAP-ENV:encodingStyle="http://www.w3.org/2001/12/soap-encoding" attribute. To do this, add an XmlAttribute to the SoapEnvelope class.
package tutorial.soap;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="envelope")
public class SoapEnvelope {
 @XmlAttribute(name="SOAP-ENV:encodingStyle")
 private String soapEnvEncodingStyle;
 
 public SoapEnvelope() {
  soapEnvEncodingStyle = "http://www.w3.org/2001/12/soap-encoding";
 }
}
That will create the following SOAP Message




Now we will create the Soap Header and the SoapBody. For this we will add two Strings to the SoapEnvelope, soapHeader and soapBody. Will initialize the strings in the constructor.
package tutorial.soap;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="envelope")
public class SoapEnvelope {
 @XmlAttribute(name="SOAP-ENV:encodingStyle")
 private String soapEnvEncodingStyle;
 
 @XmlElement(name="header")
 private String soapHeader;
 
 @XmlElement(name="body")
 private String soapBody;
 
 public SoapEnvelope() {
  soapEnvEncodingStyle = "http://www.w3.org/2001/12/soap-encoding";
  soapHeader = new String();
  soapBody = new String();
 }
}
This creates the following SOAP Message.


    
    

3 comments:

  1. I would like to thanks for this blog, keep sharing your thoughts like this because it contains more useful information...
    Soft Skills Training in Chennai
    Soft Skill Online Training

    ReplyDelete
  2. Wonderful blog!!! Thanks for sharing this great information with us...
    Applications of Data Science
    Data Science in Business

    ReplyDelete