Spring OXM - Update Project for JAXB



Update the content of pom.xml to have spring core, spring oxm and jaxb dependencies as shown below −

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.tutorialspoint</groupId>
   <artifactId>springoxm</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>Spring OXM</name>
   <description>Spring OXM Project</description>
   <properties>
      <java.version>24</java.version>
      <org.springframework.version>7.0.0-M9</org.springframework.version>
      <xml.bind.version>4.0.4</xml.bind.version>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>
   <build>      
      <plugins>
         <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
               <source>${java.version}</source>
               <target>${java.version}</target>
            </configuration>
         </plugin>
      </plugins>
   </build>
   <dependencies>
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-context</artifactId>
         <version>${org.springframework.version}</version>
         <scope>compile</scope>
      </dependency>
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-oxm</artifactId>
         <version>${org.springframework.version}</version>
      </dependency>
      <dependency>
         <groupId>jakarta.xml.bind</groupId>
         <artifactId>jakarta.xml.bind-api</artifactId>
         <version>${xml.bind.version}</version>
      </dependency>
      <dependency>
         <groupId>org.glassfish.jaxb</groupId>
         <artifactId>jaxb-runtime</artifactId>
         <version>${xml.bind.version}</version>
         <scope>runtime</scope>
      </dependency>
   </dependencies>
</project>

Create a class Student.java with O/X Annotation as shown below.

Student.java

package com.tutorialspoint.oxm.model;

import jakarta.xml.bind.annotation.XmlAttribute;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Student{
   String name;
   int age;
   int id;

   public String getName(){
      return name;
   }
   @XmlElement
   public void setName(String name){
      this.name = name;
   }
   public int getAge(){
      return age;
   }
   @XmlElement
   public void setAge(int age){
      this.age = age;
   }
   public int getId(){
      return id;
   }
   @XmlAttribute
   public void setId(int id){
      this.id = id;
   }
}

Create applicationcontext.xml in src → main → resources with the following content.

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">  

   <oxm:jaxb2-marshaller id="jaxbMarshaller">  
      <oxm:class-to-be-bound name="com.tutorialspoint.oxm.model.Student"/>  
   </oxm:jaxb2-marshaller>  
</beans>  
Advertisements