Home  >  Spring

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.
apply plugin: 'java'
apply plugin: 'eclipse'
archivesBaseName = 'concretepage'
version = '1' 
repositories {
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.
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;
public class AppConfig {
	public XStreamMarshaller getXStreamMarshaller() {
	  XStreamMarshaller xstreamMarshaller = new XStreamMarshaller();
          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.
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.
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();
	   XStreamMarshaller xstream = ctx.getBean(XStreamMarshaller.class);
	   //Perform Marshaling
	   Company company = new Company();
	   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.
Find the output for XML to Java Object.
Marshaling completed.
After UnMarshaling Data is: id:100, CountryName:PQR 

Download Complete Source Code


©2022 concretepage.com | Privacy Policy | Contact Us