This extends Basic JAXB tutorial with Maven & XSD – 1 to marshall with Spring OXM and Spring XML.
Right mouse click on target/generated-sources and select “Build Path –> Use as Source Folder”
Step 1: add spring-orm to use JAXB and spring-xml to use StringResult.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>3.2.13.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.ws</groupId> <artifactId>spring-xml</artifactId> <version>2.2.0.RELEASE</version> </dependency> </dependencies> |
Step 2: The App class with marshaling logic. It is marshaling the “PersonRequest” that was generated in the previous tutorial from an XSD file using the “maven-jaxb2-plugin”.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
package com.jaxb.tutorial; import java.io.IOException; import javax.xml.bind.JAXBException; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.oxm.Marshaller; import org.springframework.oxm.XmlMappingException; import org.springframework.xml.transform.StringResult; import com.myapp.schema.v1.dto.AddressDto; import com.myapp.schema.v1.dto.PersonRequest; public class App { private Marshaller marshaller; public static void main(String[] args) throws JAXBException, XmlMappingException, IOException { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); App myApp = (App) ctx.getBean("springApp"); myApp.process(); ((ConfigurableApplicationContext)ctx).close(); } public void process() throws XmlMappingException, IOException{ PersonRequest req = new PersonRequest(); req.setFirstName("Peter"); req.setLastName("Smith"); AddressDto address = new AddressDto(); address.setStreet("Bora Street"); address.setSuburb("Kensington"); req.setAddress(address); StringResult result = new StringResult(); marshaller.marshal(req, result); System.out.println(result.toString()); } public Marshaller getMarshaller() { return marshaller; } public void setMarshaller(Marshaller marshaller) { this.marshaller = marshaller; } } |
Step 3: The Spring context class “applicationContext.xml”
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <bean id="springApp" class="com.jaxb.tutorial.App" > <property name="marshaller" ref ="jaxb2Marshaller" /> </bean> <bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> <property name="classesToBeBound"> <list> <value>com.myapp.schema.v1.dto.PersonRequest</value> </list> </property> </bean> </beans> |
Output:
|
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <PersonRequest xmlns="http://www.myapp.com/Person"> <firstName>Peter</firstName> <lastName>Smith</lastName> <address> <street>Bora Street</street> <suburb>Kensington</suburb> </address> </PersonRequest> |
Q. How does it know how to map?
A. From the XSD generated Java classes with the JAXB annotations demonstrated in https://www.java-success.com/basic-jaxb-tutorial-with-maven-xsd-1/.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package com.myapp.schema.v1.dto; import java.io.Serializable; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; import com.jaxb.tutorial.AbstractRestDto; @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "PersonDto", propOrder = { "firstName", "lastName", "address" }) @XmlRootElement(name = "PersonRequest") public class PersonRequest extends AbstractRestDto implements Serializable { private final static long serialVersionUID = 1L; @XmlElement(required = true) protected String firstName; @XmlElement(required = true) protected String lastName; @XmlElement(required = true) protected AddressDto address; //getters & setters omitted } |
And
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package com.myapp.schema.v1.dto; import java.io.Serializable; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlType; import com.jaxb.tutorial.AbstractRestDto; @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "AddressDto", propOrder = { "street", "suburb" }) public class AddressDto extends AbstractRestDto implements Serializable { private final static long serialVersionUID = 1L; @XmlElement(required = true) protected String street; @XmlElement(required = true) protected String suburb; //getters and setters omitted } |
