Over a million developers have joined DZone.

Marshalling Java to JSON in JAX-RS

DZone's Guide to

Marshalling Java to JSON in JAX-RS

· Integration Zone ·
Free Resource

The Future of Enterprise Integration: Learn how organizations are re-architecting their integration strategy with data-driven app integration for true digital transformation.

In this article, I am going to explain about the purpose and the process of writing custom marshaller for web services. First we have to be clear on what is marshaller. Marshalling is the nothing but the process of converting in-memory object into persisting or transportable format.

Now a days most of the web services are returning the response as JSON object. Some people are still using XML as their preferred transport medium.

JAX-RS api has introduced a generic and pluggable interface called MessageBodyWriter for doing the marshalling.


We will take an use-case in-order to understand the situation little better. We have a Java bean called Book. Now we have to write a REST service which will fetch the Java object from the given ID and return JSON response back to the client.


public class Book {
	private String title;
	private String author;

	public Book(String title, String author) {
	 this.title = title;
	 this.author = author;

	public String getTitle() {
	 return title;

	public String getAuthor() {
	 return author;


public class BookResource {

	 public Book getBook(@PathParam("id") String id) {

	 //DataProvider is simple data holder
	 DataProvider dataProvider = DataProvider.getInstance();
	 return dataProvider.getBook(id);

In the above REST method, we have just returned Java object itself to the client as response. If you notice the @Produces, we are returning the JSON contents back to the client.

Stumbled! The magic happens at the marshalling layer. Now we will see how the marshalling code will look like.


public class BookJsonMarshaller implements MessageBodyWriter<Book> {

	 public long getSize(Book book, Class<?> clazz, Type type, Annotation[] annotations, MediaType mediaType) {
	 return -1;

	 public boolean isWriteable(Class<?> clazz, Type type, Annotation[] annotations, MediaType mediaType) {
	 return clazz == Book.class;

	 public void writeTo(Book book, Class<?> clazz, Type type, Annotation[] annotations, MediaType mediaType,
	 MultivaluedMap<String, Object> valueMap, OutputStream stream) throws IOException, WebApplicationException {

	 JsonObject jsonObject = Json.createObjectBuilder()
	  .add("title", book.getTitle())
	  .add("author", book.getAuthor()).build();

	 DataOutputStream outputStream = new DataOutputStream(stream);

We have to look 'writeTo' method just to understand the process. The first arguments is coming from the injection layer so the current Java object will be available to this processor. Now we have process the Java object and should generate JSON object. In this example I have used the 'javax.json.jar' for generating the JSON content. Finally the JSON content should be written into output stream.

Finally we have register the provider (BookJsonMarshaller) into our application like other resources...

Before we conclude this article, you may have one question. Why we need to write the custom marshaller for processing the Java object. Is it not possible by default? You question is valid. This is still possible. But if you want to have a full control over the generated JSON content, we have to write our own @Providers...

I hope you have enjoyed reading this article. If you have any questions or comments please reply to this thread... We will meet again with another discussion.

For code reference, don't forget to visit Github

Advance Christmas wishes to my readers!

Make your mark on the industry’s leading annual report. Fill out the State of API Integration 2019 Survey and receive $25 to the Cloud Elements store.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}