I wanted to add AJAX to Spring MVC application. So, I did what most us do, go through the documentation or blog of the Spring Source. But, after playing around I didn’t get it to work properly so here are some details that might save you some time.
After I follow the instructions in AJAX in Spring 3.0 I got some error “406 Not Accepted”, so let’s explain how to make it work:
First you need to setup the actions/methods that the ajax client will call and provide that data in a request. In the server side we are going to use Spring MVC and reply using a JSON format.
1. You need the annotation <mvc:annotation-driven /> in your spring.xml or servelet-web-context.xml
2. Then, you need to create your controller action that will reply to the AJAX invocation. Let’s see the following example. E.g. ProductController.java
There are a couple of things to point out here. Notice the return @ResponseBody Product type. So, you need to create create a POJO (Plain java class with the data that you want to send along with it’s getters and setters). E.g. Product.java.
Also, notice the @ResponseBody annotation. This annotation allow you to translate the Product object into a JSON representation. But, this is not magic! and you need a couple of JARs to make it work (additionally to the annotation metined in step (1):
Use maven or download and place this JARs in the lib manually.
On the client side, I’m using jQuery and the code looks like this:
There are some function there that are useful for debugging like printing out errors to the console and complete function. Notice also that $.getJSON is expenting the reponse in of a appplication/json type. So be sure that you have the “Accept=application/json” in your controller on the server side.
That’s all you need.
As mentioned before the spring mvc blog explain more in details each of the steps but lack some minor details that are key to make it work. I was getting “406 Not Accepted” because I didn’t have the jackson jars that the @ResponseBody needs to convert java objects to JSON. And also you need to add the Accept Request header in the controller.
Using Firebug in Firefox is very tab Net > XHR you can see all your ajax request and reponses. Very useful for debugging. Hope this save you some time and frustration. Any question or suggestion fee free to comment below or contact me.