My Quotes


When U were born , you cried and the world rejoiced
Live U'r life in such a way that when you go
THE WORLD SHOULD CRY






Saturday, March 8, 2014

Spring OXM in 2 minutes

Once you have XSD defined, here are the easy steps to generate the OXM (JAXB) classes using Spring.

You can also ensure that the XSD validations are done prior to absorbing the XML into your system.

Here are the steps to configure and code with Spring and OXM.

Firstly the pom.xml 


3.1.3.RELEASE


  
   org.springframework
   spring-oxm
   ${org.springframework.version}
  
  
   org.springframework
   spring-asm
   ${org.springframework.version}
  
  
   org.springframework
   spring-aspects
   ${org.springframework.version}
  
  
   org.springframework
   spring-aop
   ${org.springframework.version}
  

Secondly the Spring configuration

 
  
  
 

You can attach this messageConverter into any bean for example into a JMS Template as


Right click on the XSD (sample.xsd) in your eclipse and generate the JAXB classes into the package (com.oxm.test)

Last a simple method to convert the incoming XML into the OXM Objects

    private Projection convertXmlStringToMyAppPayload(final String xmlMessage) throws ApplicationException {
 try {
     return  (ClassFileFromsample.xsd) marshaller.unmarshal(new StreamSource(new StringReader(xmlMessage)));     
 } catch (Exception appex) {
     LOGGER.error("ApplicationException Found in convertXmlStringToMyAppPayload() of Service class ", appex);
 }
    }


Host Identifier and Spring

Spring and Host Identifiers

 
   
 
            
            
             Dev
            
    

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class HostIdentifier {
    @Autowired
    private String hostname;

    @Autowired
    private String region;

    public String getHostname() {
 return hostname;
    }

    public void setHostname(String hostname) {
 this.hostname = hostname;
    }

    public String getRegion() {
 return region;
    }

    public void setRegion(String region) {
 this.region = region;
    }

}

TimeStamp Adapters for JAXB

As we have seen in many cases, the Timestamp coming in as Sting, it is easier to cast it using JAXB.

Here is the TimeStampAdapter which we can use it to streamline the same

Just define the TimeStamp attribute of the JAXB Object as date as follows
    @XmlElement(name = "ActionTimestamp")
    @XmlJavaTypeAdapter(TimeStampAdapter.class)
    private Date actionTimestamp;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class TimeStampAdapter extends XmlAdapter {
    private SimpleDateFormat firstFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    private SimpleDateFormat secondTimeFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SSS");

    @Override
    public String marshal(Date v) {
 return firstFormat.format(v);
    }

    @Override
    public Date unmarshal(String v) throws ParseException {
 Date returnDate = null;
 try {
     returnDate = firstFormat.parse(v);
 } catch (Exception prse) {
     returnDate = secondFormat.parse(v);
 }
 return returnDate;
    }
}

Date and Time Adapter for JAXB

It is often very time consuming to check for the incoming String and cast it using SimpleDateFormat in every class. Here is a simple way to fasten this by using Date Adapters in JAXB itself.

All you need to do is to define the JAXB objects to be Date as follows

@XmlElement(name = "CreditValueDate")
    @XmlJavaTypeAdapter(DateTypeAdapter.class)
    private Date creditValueDate;
    @XmlElement(name = "DebitValueDate")
    @XmlJavaTypeAdapter(DateTypeAdapter.class)
    private Date debitValueDate;

Class for DateTypeAdapters

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class DateTypeAdapter extends XmlAdapter {
    private SimpleDateFormat firstDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private SimpleDateFormat secondDateFormat = new SimpleDateFormat("yyyy/MM/dd");

    @Override
    public String marshal(Date v) {
 return secondDateFormat.format(v);
    }

    @Override
    public Date unmarshal(String v) throws ParseException {
 Date returnDate = null;
 try {
     returnDate = firstDateFormat.parse(v);
 } catch (Exception prse) {
     returnDate = secondDateFormat.parse(v);
 }
 return returnDate;
    }
}

Convert Object to XML String

 public String convertObjectToXmlString(ObjectClass theObjectClass)
   throws Exception {
  Marshaller triggerMarshaller = null;
  System.out.println("Converting String into XML");
  /** Anonymous Inner Class */
  OutputStream output = new OutputStream() {
   private StringBuilder string = new StringBuilder();

   @Override
   public void write(int b) throws IOException {
    this.string.append((char) b);
   }

   @Override
   public String toString() {
    return this.string.toString();
   }
  };

  JAXBContext jc = JAXBContext.newInstance("Your Package Name");
  triggerMarshaller = jc.createMarshaller();
  triggerMarshaller.marshal(triggerMessage, new StreamResult(output));
  return output.toString();

 }