Although Java classes are annotated using annotations from the JAXB library, they will be used to generate XML data. However, in some cases, you will need to generate JSON data instead of XML using these Java classes. To do this, you can use the Jackson library with jackson-module-jaxb-annotations for the Java EE namespace, or jackson-module-jakarta-xmlbind-annotations for the Jakarta EE namespace. How is it in detail? Let’s find out together in this tutorial!
I will create a new Maven project:
for example.
I will use the Jakarta EE namespace so I will declare the JAXB and jackson-module-jakarta-xmlbind-annotations dependencies as follows:
1 2 3 4 5 6 7 8 9 10 11 |
<dependency> <groupId>jakarta.xml.bind</groupId> <artifactId>jakarta.xml.bind-api</artifactId> <version>4.0.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.module</groupId> <artifactId>jackson-module-jakarta-xmlbind-annotations</artifactId> <version>2.18.1</version> </dependency> |
As an example, I have a Student class annotated with JAXB library annotations as follows:
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 |
package com.huongdanjava.jackson; import jakarta.xml.bind.annotation.XmlElement; import jakarta.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "student") public class Student { private String name; private String age; private String code; public String getName() { return name; } @XmlElement public void setName(String name) { this.name = name; } public String getAge() { return age; } @XmlElement public void setAge(String age) { this.age = age; } public String getCode() { return code; } @XmlElement public void setCode(String code) { this.code = code; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + ", code=" + code + "]"; } } |
I will initialize a Student object with the following information:
1 2 3 4 |
Student student = new Student(); student.setAge("37"); student.setName("Khanh"); student.setCode("123"); |
To generate Student object information in JSON format, first, I will initialize the ObjectMapper object as follows:
1 2 |
ObjectMapper om = new ObjectMapper(); om.configure(SerializationFeature.WRAP_ROOT_VALUE, true); |
Next, we need to enable Jakarta XML Binding support for the ObjectMapper object using the JakartaXmlBindAnnotationIntrospector class, in one of the following two ways:
1 2 |
JakartaXmlBindAnnotationModule module = new JakartaXmlBindAnnotationModule(); om.registerModule(module); |
or:
1 2 |
AnnotationIntrospector intr = new JakartaXmlBindAnnotationIntrospector(om.getTypeFactory()); om.setAnnotationIntrospector(intr); |
and now, we will use the writeValueAsString() method of the ObjectMapper object to convert the Student object to a JSON string:
1 2 |
String json = om.writeValueAsString(student); System.out.println(json); |
My entire code is as follows:
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 |
package com.huongdanjava.jackson; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.AnnotationIntrospector; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.module.jakarta.xmlbind.JakartaXmlBindAnnotationIntrospector; public class Application { public static void main(String[] args) throws JsonProcessingException { Student student = new Student(); student.setAge("37"); student.setName("Khanh"); student.setCode("123"); ObjectMapper om = new ObjectMapper(); om.configure(SerializationFeature.WRAP_ROOT_VALUE, true); AnnotationIntrospector intr = new JakartaXmlBindAnnotationIntrospector(om.getTypeFactory()); om.setAnnotationIntrospector(intr); String json = om.writeValueAsString(student); System.out.println(json); } } |
The result when I run my example is as follows: