Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Spring JAXB Integration

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.24k
    Comment on it

    Spring JAXB Integration

    JAXB stands for Java Architechture for XML Binding. JAXB is a popular api for the Java object mapping with the xml. With JAXB we can map a Java object to it's corresponding XML document. It is also called an OXM (Object XML Mapping) or O/M framework provided by Sun.

    The Springs Object/XML Mapping, is a way to converting Java Object to XML or vice verse. This process is also known as marshaling and unmarshalling.

    1. XML Marshalling : Marshalling is called when the Java object is converted into it's corresponding XML document.

    2. XML Unmarshalling : Unmarshalling is called when the XML document is converted back to the Java object.

    POM.XML

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
            <modelVersion>4.0.0</modelVersion>
            <groupId>com.evon.jaxb.example</groupId>
            <artifactId>spring-jaxb-example</artifactId>
            <version>0.0.1-SNAPSHOT</version>
           <properties>
                    <spring.version>3.2.3.RELEASE</spring.version>
            </properties>
            <dependencies>
            <!-- Spring dependencies -->
                    <dependency>
                            <groupId>org.springframework</groupId>
                            <artifactId>spring-core</artifactId>
                            <version>${spring.version}</version>
                    </dependency>
                    <dependency>
                            <groupId>org.springframework</groupId>
                            <artifactId>spring-context</artifactId>
                            <version>${spring.version}</version>
                    </dependency>
            <!-- Spring OXM -->
                    <dependency>
                            <groupId>org.springframework</groupId>
                            <artifactId>spring-oxm</artifactId>
                            <version>${spring.version}</version>
                    </dependency>
            </dependencies>
    </project>
    

    Student.java

    package com.evon.jaxb.example; 
    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.XmlElement;
    importjavax.xml.bind.annotation.XmlRootElement;
    @XmlRootElement(name="student")
    public class Student { 
        private long id;     
        private String name; 
        @XmlAttribute(name="id")
        public long getId() { 
            return id; 
        } 
        public void setId(long id) { 
            this.id = id; 
        } 
        @XmlElement(name="name")
        public String getName() { 
            return name; 
        } 
        public void setName(String name) { 
            this.name = name; 
        } 
    
        @Override 
        public String toString() { 
            return "Student details [id=" + id + ", name=" + name + "]"; 
        }    
    } 
    

    The above is the model object which we will bind to the XML document. Now we will write a Java file which will marshal and unmarshal the Java object.

    XMLConverter.java

    package com.evon.jaxb.example; 
    
    import java.io.FileInputStream; 
    import java.io.FileOutputStream; 
    import java.io.IOException; 
    
    import javax.xml.transform.stream.StreamResult; 
    import javax.xml.transform.stream.StreamSource; 
    
    import org.springframework.oxm.Marshaller; 
    import org.springframework.oxm.Unmarshaller; 
    
    public class XMLConverter { 
        private static final String FILE = "student.xml"; 
        private Student studentObj = new Student(); 
        private Marshaller marshaller; 
        private Unmarshaller unmarshaller; 
    
        public void setMarshaller(Marshaller marshaller) { 
            this.marshaller = marshaller; 
        } 
    
        public void setUnmarshaller(Unmarshaller unmarshaller) { 
            this.unmarshaller = unmarshaller; 
        } 
    
        public void objectToXML() throws IOException { 
            FileOutputStream outputStream = null; 
            try { 
                System.out.println("--- Marshaller ---"); 
                outputStream = new FileOutputStream(FILE); 
                studentObj.setId(111); 
                studentObj.setName("Sumit"); 
                this.marshaller.marshal(studentObj, new StreamResult(outputStream)); 
                System.out.println("Student Info " + studentObj + " saved to student.xml file. "); 
            } finally { 
                if (outputStream != null) { 
                    outputStream.close(); 
                } 
            } 
        } 
    
        public void xmlToObject() throws IOException { 
            FileInputStream inputStream = null; 
            try { 
                System.out.println("--- Unmarshaller ---"); 
                inputStream = new FileInputStream(FILE); 
                this.studentObj = (Student) this.unmarshaller 
                        .unmarshal(new StreamSource(inputStream)); 
                System.out.println("Info loaded from xml : " + studentObj); 
            } finally { 
                if (inputStream != null) { 
                    inputStream.close(); 
                } 
            } 
        } 
    }
    

    In above class the function objectToXML() will convert the Student class object to the XML document called student.xml, and xmlToObject() function will convert the student.xml document back to the Java object.

    Now we will register the Student object in spring configuration file and inject the marshaller and unmarshaller object to the XMLConverter class.

    ApplicationContext.xml

     <?xml version="1.0" encoding="UTF-8"?>  
        <beans xmlns="http://www.springframework.org/schema/beans"  
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
            xmlns:oxm="http://www.springframework.org/schema/oxm"  
            xsi:schemaLocation="http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
                http://www.springframework.org/schema/oxm  
                http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd">  
                <bean id="converterBean" class="com.evon.jaxb.example.XMLConverter">
                    <property name="marshaller" ref="jaxbMarshaller" />
                    <property name="unmarshaller" ref="jaxbMarshaller" />
                </bean>
                <oxm:jaxb2-marshaller id="jaxbMarshaller">  
                    <oxm:class-to-be-bound name="com.evon.jaxb.example.Student"/>  
                </oxm:jaxb2-marshaller>  
        </beans>  
    

    MainApp.java

    package com.evon.jaxb.example; 
    import java.io.IOException; 
    import org.springframework.context.ConfigurableApplicationContext; 
    import org.springframework.context.support.ClassPathXmlApplicationContext; 
    public class MainApp { 
        public static void main(String[] args) {     
                ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 
                XMLConverter converter = (XMLConverter) context.getBean("converterBean"); 
                try { 
                    converter.objectToXML(); 
                    converter.xmlToObject(); 
                } catch (IOException e) { 
                    e.printStackTrace(); 
                }            
                context.close(); 
        } 
    } 
    

    Output :

    <?xml version="1.0" encoding="UTF-8"?>
    <info id=111><name>Sumit</name></info>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: