Monthly Archives: April 2016

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

Spring Java Config

After Spring came out with annotations based Java configuration, I found them very handy. Get rid of the xml Spring configs, as the Java configs are safe with refactoring, more readable and less verbose. I will give some of the examples that I used:

Configuration of Jdbc Connection Pool

Configuration of Spring MVC

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.

Excluding a specific class from the config

Sometimes it so happens that we would like to selectively disable a couple of classes from the annotation config. This is how it is done:

 

Sources

The sources can be found here:

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

Replacing web.xml with Java config

Post Servlet 3.0, web.xml has become redundant. It can now be replaced with pure Java configuration.

We can do that even without Spring. But since we are using Spring MVC, I am taking the Spring example. Define a class implementing the WebApplicationInitializer, as below:

Note that we can define servlets, filters, etc., whatever we do in the web.xml. Its super simple and very readable.

Please be aware that you need to include the below servlet 3.x dependency in your pom.xml:

Moreover, you have to include the below Maven plugin and set the failOnMissingWebXml property to false in pom.xml:

 

The sources will be found here:

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

Running Jetty 9 with Maven

Running Jetty through Maven is super simple. The only problem is lot of configurations were changed in Jetty 9. It is significantly different from, say, Jetty 6. I will keep it short. The below lines in pom.xml would get the job done:

Note that it defines a custom port and a context path as well.

Use the below command to run Jetty:

mvn jetty:run

The sources are available here:

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

Running Jetty 9 with Ant

We have a simple web application written in Spring MVC. Today, we will demo how to run the war through Ant, using Jetty 9. The needed libraries can either be manually downloaded and provided as a part of the project. However, sometimes, it becomes unwieldy, especially if you want to upgrade the Jetty version. So, we will define them as Maven dependencies in the build.xml.

The beauty of defining the required jars as Maven dependencies is, that these jars will be downloaded in local Maven repository if not present.

Note that we define the location of the war file. Also, we define a custom port and a context path.

The source code can be found here:

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