← Back to list

Marshalling & Unmarshalling in Java

JACKSON & JAXB for JAVA Object ↔ JSON / XML String

Niharika · 2025-03-17 16:11 · 4 claps · 2.0 min read
#marshalling #unmarshalling #xml #jaxb #jackson
Open on Medium ↗
Wiki topics: 🔭 · Astronomy & Space

Marshalling & Unmarshalling in Java

JACKSON & JAXB for JAVA Object ↔ JSON / XML String

The term “marshalling” comes from the concept of packing or preparing a data structure (Java object) for transport or communication. It involves serialization, where the data structure (POJO) is converted into a format that can be sent over a network, saved in a file, or transferred between different systems. The opposite of marshalling is unmarshalling, where you convert the formatted data (JSON/XML) back into a Java object (POJO).

Note : use try catch

Note : use try catch

JAXB (Java Architecture for XML Binding)

JAXB is a framework that allows you to convert Java objects into XML (known as marshalling) and convert XML into Java objects (known as unmarshalling).

  • Marshalling: Converting a Java object to an XML format.
  • Unmarshalling: Converting an XML format back into a Java object.

JAXB provides an easy and consistent way to handle XML in Java, so you don’t have to manually construct or parse XML strings.

Marshaller and Unmarshaller in JAXB

  • **Marshaller**: A Marshaller class is used to convert (marshal) a Java object into XML. You call the marshal method on the Marshaller instance to convert your Java object into an XML string or output stream.
JAXBContext context=JAXBContext.newInstance(MyClass.class);
Marshaller marshaller = context.createMarshaller(); 
marshaller.marshal(myObject, new StringWriter());
  • **Unmarshaller**: An Unmarshaller does the opposite: it converts (unmarshals) an XML string into a Java object.
JAXBContext context=JAXBContext.newInstance(MyClass.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
MyClass myObject = (MyClass) unmarshaller.unmarshal(new StringReader(xmlString));

Set Up JAXB Context

JAXB uses a JAXBContext to manage the conversion between Java objects and XML. You create a JAXBContext instance for a particular class (or set of classes).

Dependencies :

JACKSON: The **spring-boot-starter-web starter already includes Jackson (jackson-databind), so you don’t need to add it manually** unless you need extra features.

JAXB:

<dependency>
    <groupId>jakarta.xml.bind</groupId>
    <artifactId>jakarta.xml.bind-api</artifactId>
    <version>4.0.0</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
    <version>4.0.4</version>
</dependency>

메타데이터
post_id
bfc153cbac6e
slug
marshalling-unmarshalling-in-java-bfc153cbac6e
url
https://medium.com/@bhargavineha2002/marshalling-unmarshalling-in-java-bfc153cbac6e
canonical_url
https://medium.com/@bhargavineha2002/marshalling-unmarshalling-in-java-bfc153cbac6e
author_url
https://medium.com/@bhargavineha2002
status
ok
fetched_at
2026-07-20 15:35:23