Over a million developers have joined DZone.

Marshalling Java to JSON in JAX-RS

· Integration Zone

Build APIs from SQL and NoSQL or Salesforce data sources in seconds. Read the Creating REST APIs white paper, brought to you in partnership with CA Technologies.

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!

The Integration Zone is brought to you in partnership with CA Technologies.  Use CA Live API Creator to quickly create complete application backends, with secure APIs and robust application logic, in an easy to use interface.


Published at DZone with permission of Thamizh Arasu, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}