Category Archives: spring

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:


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.


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


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:


For xml, we can similarly say:



The sources can be found here:

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:



The sources can be found here:

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:

Example of WebSocket/STOMP using Java

WebSockets enable 2-way, duplex communication between client and server. All major browsers and all major Java servers like Jetty 9, Tomcat 7, etc. support websockets. The present example has been done with Tomcat 8 as a server and a Tyrus based simple Java client.

Simple WebSocket Server

We will use the WebSocket API to create a simple Server.

Configuration: pom.xml

The pom would be the same as a typical JEE pom. Just put the following dependency:

Server Code

Simple WebSocket Java Client

Configuration: pom.xml

The pom would be a simple one, just add these following dependencies:

Client Code

Define an Endpoint:

Then invoke this:

Note that there is a 2-way communication from server/client. This is handled in the MessageHandler defined in SimpleClientEndpoint:

Spring WebSocket Handler Server

Though the above example is simple and good for starters, this is not practical. For one, it does not integrate with Spring. Lets build something more closer to the real world.


The client attempts to invoke the BankDetailsService asynchronously, through websockets. For this, it first initiates the request, sending the server the sort-order. The server then triggers a dao-call. Each time when a new row is read off the ResultSet, it is immediately sent to the client. After all the records have been read, the session is closed. Note that the messages are binary. Json strings are gzipped and then sent across the wire to save bandwidth.

We will use the Spring websocket module for integrating WebSockets with Spring.

Configuration: pom.xml

We would need the spring websocket module:

Spring Configuration

The ExceptionWebSocketHandlerDecorator is a decorator for handling any runtime exception gracefully by closing the connection.

Server Code

This is how the BankDetailsWebSocketHandler looks like:

Note that the actual action happens in DataListenerForStompHandlerImpl:

This class is passed to the Dao, and after each record is read from the ResultSet, it is sent to the client.

Client Code

Let us take a look at the client. We are still using a standard WebSocket client, not the Spring stomp client.

Like the 1st example, we have defined our Endpoint as below:

All subsequent messages from the server would flow into the MessageHandler defined in the above class. The above is now invoked here:

Source Code


Annotation based Spring MVC, how tos

I will list here the common how to-s for the new Annotation based Spring MVC. Please note that I am using Spring version 3.0.0.RELEASE. This is a bit, only a small bit, different from the version 2.5.x. So, if you are using 2.5.x, you have to tweak things a bit before you get the same effect.

Consider the following screen:


The fantatstic thing about Spring MVC is it lets you take an Object Oriented approach while designing the Model (Form Bean) and lets you bind that seamlessly to the View (JSP Form).  For the above scenario, we can break down the top section into a list of rows. I would denote the row with a class having select, itemName, price… attributes, as shown below (this is an inner class in my ItemBean class, which is my Form Bean):

Then, in my ItemBean class, I can have a List of ItemRow, as shown:

How do I display/bind my Model to View?

This is how the View (Item.jsp) looks like:

Especially note the


in the form:checkbox tag. This is the key. It binds a particular index of the items in the ItemBean class to the JSP.

How to map a given url to my Controller method

After the introduction of Annotation @Controller, all Spring MVC controllers have become like MultiActionController, that is, you can handle multiple requests in the same controller. Mapping a url is very simple: you should annotate any public method with the @RequestMapping(“/”). Of course, you should make sure:

1.> The class is annotated with @Controller
2.> The following line is present in the Spring context file:

The method can have a motley combination of parameters and return type. Read the javadocs for details. This annotation takes an optional parameter method, which can be any enum RequestMethod type. If you do not specify anything, your method will handle all types of requests like GET, POST, DELETE, etc.

How do I pre-load or customise my Form Bean before it reaches my controller method?

In Spring MVC, the framework creates an instance of the Form Bean and binds it to the fields in JSP in case of a POST. In many cases, I would like to customise the Form Bean before it starts binding to the JSP fields. In older versions of Spring MVC, this was done by overriding the formBackingObject() method in AbstractFormController.
With annotations, you do it by specifying the @ModelAttribute before the FormBean attribute in the request handler method and then again on a public method returning the customised Form Bean as shown below:

Note that its always a good practice to specify the name in the @ModelAttribute annotation. This way you can pre-load different beans for different methods. As shown in the example above, the name specified in the @ModelAttribute annotation in the request handler method checkout() and that in the method initBeanForPost() should be an exact match. By the way, this works for GET as well as POST or any other request type.

How do I do the validation?

Spring version 3.x onwards, doing validations have become very easy. I will describe this in the following few steps:

1. Write a Validator

Your validator should implement the Validator interface. It has two methods that you need to override. The supports() method makes sure that the Validator can validate a given FormBean. The validate() method does the actual job of validating. A typical Validator will look like this:

2. Set the Validator

You do this in a public method annotated with @InitBinder in the controller.

I am setting the validator inside the if (binder.getTarget() instanceof ItemBean) block as otherwise I get an ugly exception in case there are any validation errors. I am putting the stack trace below:

java.lang.IllegalStateException: Invalid target for Validator [com.swayam.demo.web.controller.ItemController$1@11e7cc6]: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object ‘postBean’ on field ‘totalPrice’: rejected value [0.0]; codes [noItemsSelected.postBean.totalPrice,noItemsSelected.totalPrice,noItemsSelected.float,noItemsSelected]; arguments []; default message [null]] with root cause
java.lang.IllegalStateException: Invalid target for Validator [com.swayam.demo.web.controller.ItemController$1@11e7cc6]: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object ‘postBean’ on field ‘totalPrice’: rejected value [0.0]; codes [noItemsSelected.postBean.totalPrice,noItemsSelected.totalPrice,noItemsSelected.float,noItemsSelected]; arguments []; default message [null]
at org.springframework.validation.DataBinder.setValidator(
at com.swayam.demo.web.controller.ItemController.initBinder(
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(
at sun.reflect.DelegatingMethodAccessorImpl.invoke(
at java.lang.reflect.Method.invoke(
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(
at org.springframework.web.servlet.DispatcherServlet.doDispatch(

3. Use @Valid before the Form Bean

This is a new feature added in release 3.x. Now the validation happens with JSR-303 Bean Validation API. You can find the jar here. If you are using Maven, you can add the following dependency:

This is how you use it:

The Form Bean is validated with the set Validator by the framework. Note that for Spring 2.5.x, you need to make the call to validator manually inside the handler method:

In my Validator, how do I use messages from properties file for i18n?

Lets say, my properties file is located at /com/swayam/demo/web/res/. The contents are:

noItemsSelected=You have not selected any items

I have to create an instance of MessageSource in my Spring conf file as shown:

Now I can use the property key in my validate() method as shown:

How to display the Validation messages on my JSP?

First, from the controller, you have to pass an instance of BindingResult to the JSP.

And this is how the JSP looks like:

How do I bind a complex object to my JSP form field?

Often we have to display a complex object as text. The best example I can take is that of a Date. Its a java.util.Date in the model. How do I map this to a text field in my JSP? You have to extend the PropertyEditorSupport and override the following methods:
1. getAsText(): Converts an Object to its String representation. Used for displaying a model object in the JSP.
2. setAsText(String text): Converts the text from the input field to the complex object that the model understands.
A typical implementation would look like this:

Then, in the @InitBinder method, you need to register this against the class:

Putting it all together

You will find the sources here. Its an Eclipse project, using Maven. In order to get all the libraries, install Maven and run:
mvn eclipse:clean eclipse:eclipse -DdownloadSources=true -DdownloadJavadocs=true -Dwtpversion=2.0
You can also run the war file directly and go to http://localhost:8080/SpringMVC/ to see it in action.

Integrating Spring (MVC) and BlazeDS (flex)


I am not an expert in either Spring MVC or Flex and this article is not about fundamentals of Flex/BlazeDS. Its just how to get it working. Quickly.


I was playing around with Flex for the past couple of days. Being a Java developer, I was naturally inclined towards trying out BlazeDS so that I can use Flex as a front-end to my Java backend. The thing that I suffered from most was a huge information deluge. I was flooded by loads of blogs, articles and tutorials which made my life miserable. Finally, I saw a saviour in: Bare Bones BlazeDS Object Remoting, which explains how to create a BlazeDS and Java integration. Of course this does not include spring integration. Then I downloaded Spring BlazeDS, and went through the samples. Then I kind of combined these two and figured out how to make it work. The challenge here is that I have existing screens which are pure Spring MVC stuff. I am trying to embed flex in some of them, and have BlazeDS integration with my existing spring beans. Read on…

First things first: directory structure

My project directory looks like:



  1. src/main/java contains all my java files
  2. src/main/resources has all config files like hibernate.cfg.xml, property files, etc.
  3. src/main/webapp has the WEB-INF folder and the jsps
  4. src/main/webapp/flash has all the compiled .swf files to be embedded in the jsps
  5. src/main/flex has all the .mxml and action script files

Config files required for BlazeDS

You would need these three config files under the WEB-INF/flex directory:


This is the most important file. This defines the various channels that would be used for client/server communication by the BlazeDS


This defines the remoting adapter used by the flex client.

BlazeDS/Spring integration

To integrate Spring with BalzeDS, you have to touch upon these existing files:


1. Adding the flex listener

Add the flex.messaging.HttpFlexSession listener.

2. URL mapping

Note that in the services-config.xml, the url of the default channel my-amf is http://{}:{server.port}/{context.root}/spring/messagebroker/amf. You have to pass any url with this pattern to the Spring front end handler servlet, the org.springframework.web.servlet.DispatcherServlet. I assume that you already have a similar servlet defined for your Spring MVC. You should use the same servlet (so that you can re-use the same beans in flex) to map these urls. I am assuming the name of the existing DispatcherServlet is dispatcher.

This is how the modified web.xml looks like:


This xml already has the existing Spring MVC beans. This has to be modified to expose existing beans to the BlazeDS remoting as services. The xml header has to be modified as follows:

Then, the body has to be modified by adding:

Finally, this is how you expose a bean as a service for remoting:

This is how the AccountGroupManager looks like:

Coding the mxml

I am using eclipse to code the NewAccountGroup.mxml. This is how it looks like:

Compiling mxmls to swf

The one thing you have to keep in mind is that, you have to compile the mxmls along with the services-config.xml. Note that you have something like url=”http://{}:{server.port}/{context.root}/spring/messagebroker/amf” in there. While the and server.port will be taken by default by the client, you have to specify the context.root during the compile time, as an argument. This is the context of your webapp. I am using the mxmlc that comes with the Flex SDK from the command prompt (you can add it to the PATH). First you cd to the src/main/flex directory. Here you go:

mxmlc -services ../webapp/WEB-INF/flex/services-config.xml -strict=true -debug=true -context-root=/ -show-actionscript-warnings=true -output ../webapp/flash/NewAccountGroup.swf NewAccountGroup.mxml

Note how you specify the services-config.xml.

So far so good. Now we will have to embed the generated flash into our jsp. This how it is done:

Finally, just a word for Maven users, these are the arifacts you have to use:

For others, you can always expand the blazeds.war and put all the jars inside the WEB-INF/lib.

The only pain point for this approach is that every time you change your context root, you have to recompile all your flex against the services-config.xml along with a new context.root argument.

This is how the end screen looks like (in the browser):


How to pass an object from the controller to the view in SpringMVC?

The scenario is: I want to show a list of items on my view from the database. My controller picks up the data, passes it to the view which displays it. I am using a sub class of org.springframework.web.servlet.mvc.SimpleFormController:

public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {

ModelAndView view = new ModelAndView(“complaintListings”);
List<String> strings = new ArrayList<String>();

//set the object to view
view.addObject(“testStrings”, strings);

return view;


The view looks like this:

<c:forEach var=”string” items=”${testStrings}”>
<c:out value=”${string}”/>
<br />

Its that simple!