About Roger Hughes

Spring MVC, Ajax and JSON Part 2 – The Server Side Code

In my last blog I said that I was going to talk about Spring, Ajax and JSON, but didn’t. The reason for this is that I wanted to set the scene using a (barely) credible shopping web site scenario. In this scenario when the user clicks on the eCommerce page link, the server app loads some the items from a catalogue and displays them on the page. The user then checks a number of items and presses ‘Confirm Purchase’. Now, this is where Ajax and JSON come in, on pressing ‘Confirm Purchase’ the browser makes an Ajax request to the server sending it the item ids. The server then retrieves the items from the database returns them as JSON to the browser. The browser then processes the JSON, displaying the items on he screen.

My last blog got as far as creating and displaying a form that presented a list of items

from the imaginary catalogue to the user. This blog takes a look at the next step in the project: creating some JSON.

The Guys at Spring have been busy working on Ajax and JSON over the last couple of years and, as you’d expect, they do a lot of the work for you in the background. This means that all you need to do is to define a simple bean class that Spring can turn into JSON and write some controller code. In this case that class that Spring will convert to JSON is the OrderForm class:

The code above is all that’s required to return some JSON to the browser and you can see that there’s not that much to it. Firstly, the method’s @RequestMapping annotation, using the confirm and RequestMethod.POST values, maps the form attributes from myprevious blog to this method.

The modelAttribute annotation tells Spring to create and map a userSelections object from the forms posted data and inject it into the confirmPurchases(...) method’s userSelections argument. The UserSelections class is a convenience class that wraps a list of Strings. Although an example of the Lazy Class anti-pattern, this class is used to effortlessly integrate with Spring’s <form:checkbox> tag and in a real world application would contain more attributes.

The confirmPurchases(...) method converts a UserSelections input object into an OrderForm output object that’s passed back to the browser as JSON. The OrderForm object is created by looping through the list of Item ids held in the UserSelection object and looking up the corresponding Items using the fake catalogue service. Once it has the list of Items it then creates a unique purchase id using Java’s UUID class. It then passes the Items list and the purchase ID to the OrderForm‘s constructor and the order form is then passed back to Spring. Don’t forget the @ResposeBody annotation, which tells Spring to bind the OrderForm to the HTTP response body using a suitable HttpMessageConverter. This is where the magic comes in. As you may guess, a HTTP response body needs to include data that is of the correct media type to send over the internet and OrderForm definitely doesn’t fit that bill. To fix the problem it seems that Spring takes a look at the project config for suitable ways of converting the OrderForm object where it finds the jackson-core and jackson-databind libraries that were added to the project in the last blog.

In the absence of any other suitable candidates, uses these libraries to convert the OrderForm object to JSON. All of which means that you and I don’t actually have to do any real coding to produce our JSON output. Pretty clever huh!

Obviously, all this magical jiggery-pokery that’s going on in the background hides away the actual JSON output so, I find it useful to create a simple unit test similar to the one shown below:

You may argue that this isn’t a real test as it doesn’t assert anything. The value of this test is to give a visual representation of the JSON output and to ensure that the object you’re attaching to the HTTP response body can be converted into JSON by the Jackson parser. If it can’t then when you run this test you’ll get an exception.

Join Us

With 1,240,600 monthly unique visitors and over 500 authors we are placed among the top Java related sites around. Constantly being on the lookout for partners; we encourage you to join us. So If you have a blog with unique and interesting content then you should check out our JCG partners program. You can also be a guest writer for Java Code Geeks and hone your writing skills!

Disclaimer

All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners. Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.