Welcome to Spring Restful Web Services XML and JSON example. Sometime back I wrote an article about Spring REST JSON and I got a lot of comments asking how to change the program to support XML. I got some emails also asking how to make application supports both XML as well as JSON.

Spring REST XML and JSON

I thought to write an article about Spring REST XML and JSON application where I will show you how easily we can extend the existing application to support XML. Since I will be making changes to the existing project, make sure you first download it from below link.

Now do following changes to spring bean configuration file.

  1. Define a bean of type Jaxb2RootElementHttpMessageConverter.
  2. Add above configured bean to RequestMappingHandlerAdapter property messageConverters.

After above changes, our final spring bean configuration file will look like below.


We know that for working with JAXB marshalling for a class, we need to annotate it with @XmlRootElement annotation. So add this to our Employee model class.


That’s it, we are DONE. Our Spring application will support both JSON as well as XML. It will even support XML request with JSON response and vice versa. Below are some of the screenshots showing this in action.

NOTE: I am using Postman Chrome application for this, you can use any rest client for this testing.

1. XML Response: Make sure you pass Accept header as “application/xml”.


2. JSON Response: Make sure you pass Accept header as “application/json”.


3. XML Request with JSON Response: Make sure Accept header is “application/json” and Content-Type header is “text/xml” as shown in below images.


That’s all for Spring Restful web services example for supporting both XML and JSON. You can see how easy it is to extend Spring framework, this is one of the major reason of spring framework popularity.

By admin

Leave a Reply

%d bloggers like this: