Home  >  Spring Core

Spring OXM + XStream Integration Annotation Example with XStreamMarshaller

By Arvind Rai, March 30, 2015
In this page, we will learn Spring OXM and XStream integration annotation example with XStreamMarshaller. Spring provides XStreamMarshaller API for XStream marshaling and unmarshaling of XML. XStreamMarshaller can be used without any configuration. We do not require any mapping for it. It can marshal any class in classpath. For the security purpose we can assign the supported classes to XStreamMarshaller. XStream can be used as XML serialization library but not as data binding. In our example we are providing complete example of marshaling and unmarshaling of XML.

Software Used

Find the software and tool we are using to develop our demo.
1. Java 7
2. Eclipse
3. Gradle
4. Spring-Oxm:4.1.5.RELEASE

Gradle File

Find the Gradle file to resolve JAR dependencies.
build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
archivesBaseName = 'concretepage'
version = '1' 
repositories {
    mavenCentral()
}
dependencies {
	compile 'org.springframework.boot:spring-boot-starter:1.2.2.RELEASE'
	compile 'org.springframework:spring-oxm:4.1.5.RELEASE'
	compile 'com.thoughtworks.xstream:xstream:1.4.8'
}  

Configuration Class for XStreamMarshaller using Annotation

For spring and XStream integration, spring provides XStreamMarshaller. To covert java object to/from XML, we need not any configuration with XStreamMarshaller. But for security purpose, we can assign supported classes to it. We also does not require any mapping at java class label for XML element name.
AppConfig.java
package com.concretepage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.xstream.XStreamMarshaller;
import com.concretepage.bean.Company;
@Configuration
public class AppConfig {
	@Bean
	public XStreamMarshaller getXStreamMarshaller() {
	  XStreamMarshaller xstreamMarshaller = new XStreamMarshaller();
	  xstreamMarshaller.setSupportedClasses(Company.class);
          return xstreamMarshaller;
	}
} 

Java Class to be Converted To/From XML

Find a java class which will be converted into XML. We will use same XML to convert it into java object.
Company.java
package com.concretepage.bean;
public class Company {
	private Integer id;
	private String companyName;
	private String ceoName;
	private Integer noEmp;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getCompanyName() {
		return companyName;
	}
	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}
	public String getCeoName() {
		return ceoName;
	}
	public void setCeoName(String ceoName) {
		this.ceoName = ceoName;
	}
	public Integer getNoEmp() {
		return noEmp;
	}
	public void setNoEmp(Integer noEmp) {
		this.noEmp = noEmp;
	}
} 

Run Application

To test our application, we are creating an object of our java bean and calling marshal() method of XStreamMarshaller and we get a XML file as country.xml. Now this XML file is converted into Java class using unmarshal() method of XStreamMarshaller.
RunApplication.java
package com.concretepage;
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.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.oxm.xstream.XStreamMarshaller;
import com.concretepage.bean.Company;
public class RunApplication {
	public static void main(String[] args) throws IOException {
	   AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	   ctx.register(AppConfig.class);
	   ctx.refresh();
	   XStreamMarshaller xstream = ctx.getBean(XStreamMarshaller.class);
	   //Perform Marshaling
	   Company company = new Company();
	   company.setId(100);
	   company.setCompanyName("PQR");
	   company.setCeoName("MNO");
	   company.setNoEmp(50);
	   xstream.marshal(company, new StreamResult(new FileOutputStream("country.xml")));
	   System.out.println("Marshaling completed.");
	   //Perform UnMarshaling
	   company = (Company)xstream.unmarshal(new StreamSource(new FileInputStream("country.xml")));
	   System.out.println("After UnMarshaling Data is: id:"+ company.getId()+", CountryName:"+company.getCompanyName());
	}
} 
Find the output for Java object to XML.
<com.concretepage.bean.Company><id>100</id><companyName>PQR</companyName><ceoName>MNO</ceoName><noEmp>50</noEmp></com.concretepage.bean.Company>
 
Find the output for XML to Java Object.
Marshaling completed.
After UnMarshaling Data is: id:100, CountryName:PQR 

Download Complete Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us