Building a REST Server with Spring MVC

We would like to build a REST server with Spring MVC. It should be very simple to support various formats like JSON and XML for the same request, just by changing the Content header. Example, I have the below url:

http://localhost:8090/simple-spring-rest/bank-detail

It should return me either json or xml or some other format depending on the Accept header to application/json or application/xml respectively.

Lets see how to achieve that.

Configuration of Spring MVC

We will use pure Java configuration:

Note the use of WebMvcConfigurerAdapter. It comes in handy when you want to work with Spring MVC. Especially note worthy is the configureMessageConverters() method. You would use that to configure a REST service. It would define how Spring handles the @ResponseBody or @RestController annotation, to translate a POJO to the response type: json, xml, etc. In the above example, we are using MappingJackson2HttpMessageConverter to convert our POJOs to JSON and Jaxb2RootElementHttpMessageConverter to convert them to XML.

Model

Note the use of @XmlRootElement. This is absolutely necessary as we are using Jaxb2RootElementHttpMessageConverter to convert our POJOs to xml. If you omit this, you will get a “Error 406 Not Acceptable” error, the underlying cause being:

org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

Controller

The controller is very simple, and returns the POJO. It is upto the HttpMessageConverter to make sense of it and convert that to either json or xml.

This makes the perfect sense, as the controller can just return the model, and the conversion can be a configuration detail.

Alternate ways of specifying the desired response type

Spring gives us the flexibility of doing away with the Accept header to specify the type of response. If we want json output, we can simply say:

http://localhost:8090/simple-spring-rest/bank-detail.json

For xml, we can similarly say:

http://localhost:8090/simple-spring-rest/bank-detail.xml

Sources

The sources can be found here:

https://github.com/paawak/blog/tree/master/code/simple-spring-rest

Leave a Reply

Your email address will not be published. Required fields are marked *