Spring Framework Tutorial
Software Setup and Configuration (STS/Eclipse/IntelliJ)
Core Spring
Spring Annotations
Spring Data
Spring JDBC
Spring Security
Java Architecture for XML Binding (JAXB) provides a convenient way to bind XML schemas and Java representations. JAXB allows Java developers to perform operations such as reading XML (unmarshalling) and converting Java objects to XML (marshalling).
Spring Framework doesn't have any specific JAXB module because JAXB can be easily integrated with any Java application without much setup. However, Spring MVC and Spring Boot provide out-of-the-box support for JAXB when it comes to converting response and request payloads.
Here's how you can integrate JAXB with a Spring application:
For a Maven project, add the following dependency:
<dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.1</version> </dependency>
If you're using Java 9 or above, JAXB is no longer included in the JDK, so you'd need this dependency.
You annotate your classes with JAXB annotations to specify how they should be converted to and from XML.
@XmlRootElement public class User { private String name; private int age; // getters and setters... // default constructor is required by JAXB public User() {} }
Using JAXBContext
, you can convert Java objects to XML and vice versa.
// Convert Java object to XML (marshalling) JAXBContext context = JAXBContext.newInstance(User.class); Marshaller marshaller = context.createMarshaller(); StringWriter writer = new StringWriter(); marshaller.marshal(user, writer); String userXml = writer.toString(); // Convert XML back to Java object (unmarshalling) Unmarshaller unmarshaller = context.createUnmarshaller(); User userFromXml = (User) unmarshaller.unmarshal(new StringReader(userXml));
When you're using Spring MVC, you can take advantage of @ResponseBody
and @RequestBody
annotations to automatically marshal and unmarshal XML.
For this, you'll need the Spring's XML message converter. In Spring Boot, this converter is auto-configured when you have the JAXB dependency on the classpath.
Here's a sample Spring MVC controller:
@RestController @RequestMapping("/users") public class UserController { @GetMapping(produces = MediaType.APPLICATION_XML_VALUE) public User getUser() { return new User("Alice", 30); // this will be automatically converted to XML } @PostMapping(consumes = MediaType.APPLICATION_XML_VALUE) public String createUser(@RequestBody User user) { // process user return "User created!"; } }
Ensure your request headers (Accept
for GET requests and Content-Type
for POST requests) are set to application/xml
when interacting with these endpoints.
In conclusion, integrating JAXB with Spring is relatively straightforward, especially if you're using Spring Boot. You mainly deal with annotations and rely on Spring's infrastructure to handle the actual conversion process.
Configuring JAXB with Spring framework:
Jaxb2Marshaller
bean in your Spring configuration.@Configuration public class AppConfig { @Bean public Jaxb2Marshaller jaxb2Marshaller() { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setPackagesToScan("com.example.domain"); return marshaller; } }
Using JAXB in Spring MVC applications:
@Controller public class MyController { @Autowired private Jaxb2Marshaller jaxb2Marshaller; @RequestMapping(value = "/xmlEndpoint", produces = MediaType.APPLICATION_XML_VALUE) @ResponseBody public MyObject handleXmlRequest() { // Process and return MyObject } }
Spring and JAXB example for XML processing:
@Service public class MyService { @Autowired private Jaxb2Marshaller jaxb2Marshaller; public String convertObjectToXml(MyObject myObject) { StringWriter writer = new StringWriter(); jaxb2Marshaller.marshal(myObject, new StreamResult(writer)); return writer.toString(); } public MyObject convertXmlToObject(String xml) { return (MyObject) jaxb2Marshaller.unmarshal(new StringSource(xml)); } }
Marshalling and Unmarshalling XML with Spring and JAXB:
Jaxb2Marshaller
to perform marshalling and unmarshalling of XML.public class XmlProcessor { @Autowired private Jaxb2Marshaller jaxb2Marshaller; public String marshalObjectToXml(Object object) { StringWriter writer = new StringWriter(); jaxb2Marshaller.marshal(object, new StreamResult(writer)); return writer.toString(); } public Object unmarshalXmlToObject(String xml) { return jaxb2Marshaller.unmarshal(new StringSource(xml)); } }
Integrating JAXB with Spring Boot:
spring-boot-starter-jaxb
dependency.<!-- pom.xml --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jaxb</artifactId> </dependency>
Customizing JAXB in Spring applications:
Jaxb2Marshaller
with various properties.@Configuration public class AppConfig { @Bean public Jaxb2Marshaller jaxb2Marshaller() { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setPackagesToScan("com.example.domain"); marshaller.setMarshallerProperties(Collections.singletonMap( "com.sun.xml.bind.namespacePrefixMapper", new MyNamespacePrefixMapper() )); return marshaller; } }
How to map Java objects to XML using Spring and JAXB:
public class XmlProcessor { @Autowired private Jaxb2Marshaller jaxb2Marshaller; public String convertObjectToXml(Object object) { StringWriter writer = new StringWriter(); jaxb2Marshaller.marshal(object, new StreamResult(writer)); return writer.toString(); } }
Working with XML data in Spring using JAXB:
@RestController public class XmlController { @Autowired private XmlProcessor xmlProcessor; @GetMapping("/xmlEndpoint") public ResponseEntity<String> handleXmlRequest() { MyObject myObject = // ... retrieve or create MyObject String xml = xmlProcessor.convertObjectToXml(myObject); return ResponseEntity.ok(xml); } }