In this example we will see how we can convert a Java Object into an JSON using JAXB Marshalling Technique.
JAXB Dependency
We will be requiring the below jars to be put in the classpath for performing the marshalling operation.
- jaxb-api.jar
- org.eclipse.persistence.moxy-2.6.2.jar
- eclipselink-2.6.2.jar
- validation-api-1.1.0.Final.jar
Important Note :
In order to configure MOXY as JAXB provider we need to create a file named “jaxb.properties” in the same package where the model(Student.java) exist with the below content
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
If you have missed to add the jaxb.properties file then you will be getting the below exception
javax.xml.bind.PropertyException: name: eclipselink.media-type value: application/json at javax.xml.bind.helpers.AbstractMarshallerImpl.setProperty(AbstractMarshallerImpl.java:358) at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.setProperty(MarshallerImpl.java:527) at com.javainterviewpoint.jaxb.Object_JSON_Example_Moxy.main(Object_JSON_Example_Moxy.java:24)
Student.java
Our Student class is a simple POJO class containing three properties name,age,id. We will be using 2 main annotations namely @XmlRootElement(Maps Class to XML element) and @XmlElement(Maps Bean property to XML element).
package com.javainterviewpoint.jaxb; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class Student { String name; String age; int id; 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 int getId() { return id; } @XmlElement public void setId(int id) { this.id = id; } }
ObjectToJSON_Example.java
package com.javainterviewpoint.jaxb; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import org.eclipse.persistence.jaxb.MarshallerProperties; public class ObjectToJSON_Example { public static void main(String args[]) { Student st = new Student(); st.setName("JavaInterviewPoint"); st.setAge("11"); st.setId(12); try { //Create jaxbContext JAXBContext jaxbContext = JAXBContext.newInstance(Student.class); //Getting Marshaller object Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); //Setting Marshaller MediaType to json jaxbMarshaller.setProperty("eclipselink.media-type", "application/json"); //set it to true for printing the root element jaxbMarshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true); //set it to true for producing a formatted output jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); //printing the json in the console jaxbMarshaller.marshal(st, System.out); } catch (JAXBException e) { e.printStackTrace(); } } }
We will be performing the below steps to convert Object into JSON
- Create object for our Student class and set values to the property associated with it.
- JaxbContext is created by passing the class reference of the Student class.
- Call the createMarshaller() method of the context above created, to get the object of the Marshaller
- Set the Marshaller MediaType to JSON
jaxbMarshaller.setProperty("eclipselink.media-type", "application/json");
- Set MarshallerProperties.JSON_INCLUDE_ROOT as true so that the output will be including the root element and the output will be like below
{ "student" : { "age" : "11", "id" : 12, "name" : "JavaInterviewPoint" } }
- If MarshallerProperties.JSON_INCLUDE_ROOT is set as false, then the root element will not be included.
{ "age" : "11", "id" : 12, "name" : "JavaInterviewPoint" }
- Set Marshaller.JAXB_FORMATTED_OUTPUT as true, if this is not set then the output will be produced without formatting(In a single line)
{"student":{"age":"11","id":12,"name":"JavaInterviewPoint"}}
- Finally the marshal() method is called which converts the Student object into JSON.
Output :
{ "student" : { "age" : "11", "id" : 12, "name" : "JavaInterviewPoint" } }
Leave a Reply