Posted on by & filed under Content - Highlights and Reviews, Web Development.

Spring MVC is an implementation of the Model-View-Controller pattern for the Web. Providing Web services requires building code which speaks potentially many protocols, such as JSON and XML. The modular architecture of Spring MVC allows translation from these protocols to simple Java classes to be layered transparently on top of controller implementations. This tip will show how to cleanly support both JSON and XML in a Spring MVC application.

The source code for this article is available at

Java Architecture for XML Binding

JAXB includes a useful collection of annotations for decorating Java classes with information about how they map to and from XML. There are multiple JAXB implementations, including the core Java runtime as of version 1.6.

There are several JAXB annotations, but a only key few are needed to build a robust XML (and JSON) data model.

The @XmlRootElement annotation specifies that a class can be a top-level XML element, and not only a nested XML element.

The @XmlAccessorType annotation specifies how fields should be accessed by the JAXB implementation. Here, fields are accessed directly, rather than through accessor methods.

The @XmlAttribute and @XmlElement annotations specify data within the XML element, as properties of the XML element and nested XML elements, respectively.

This configuration will map to an XML document such as the following.


Jackson is a useful library for parsing and binding JSON against Java objects. One of its handy features is the ability to use JAXB annotations to configure JSON serialization. This is optional, but useful as no changes are requred to the Comment class to control JSON support in the application.

For another example of using Jackson with Spring MVC, look at Spring in Action, Third Edition in Safari Books Online.

Using the same JAXB annotations as before, a JSON document created from a Comment will look very similar to its XML counterpart.

Content-based view resolution

Supporting both XML and JSON in the same Web application means that a user needs to be able to specify which format they would like to use. In idiomatic HTML, this specification occurs in the Accept and Content-Type HTTP headers.

Spring MVC supports this approach with the ContentNegotiatingViewResolver, which can be configured with a collection of views and view resolvers, to be selected at HTTP request time based on the user’s Content-Type header.

Here, two views are configured: one to provide JAXB serialization, and the other to provide JSON serialization. These are added as default views to a ContentNegotiatingViewResolver instance, which is returned to Spring MVC as the single ViewResolver instance.

A simple test of these two views can be made with cURL.

Safari Books Online has the content you need

Below are some Spring books to help you develop applications, or you can check out all of the Spring books and training videos available from Safari Books Online. You can browse the content in preview mode or you can gain access to more information with a free trial or subscription to Safari Books Online.

Pro Spring MVC: With Web Flow provides in-depth coverage of Spring MVC and Spring Web Flow, two highly customizable and powerful web frameworks brought to you by the developers and community of the Spring Framework.
Totally revised for Spring 3.0, Spring in Action, Third Edition is a hands-on guide to the Spring Framework. It covers the latest features, tools, and practices including Spring MVC, REST, Security, Web Flow, and more.
The release of Spring Framework 3 has ushered in many improvements and new features. Spring Recipes: A Problem-Solution Approach, Second Edition continues upon the bestselling success of the previous edition but focuses on the latest Spring 3 features for building enterprise Java applications.
Spring Batch in Action is an in-depth guide to writing batch applications using Spring Batch. Written for developers who have basic knowledge of Java and the Spring lightweight container, the book provides both a best-practices approach to writing batch jobs and comprehensive coverage of the Spring Batch framework.
Spring Web Services 2 Cookbook includes a wide variety of recipes that covers most important topics used in real-world applications. It is a well-rounded guide covering a lot of ground in the Spring Web Services domain using systematic arranged chapters and focused recipes.

Start your FREE 10-day trial to Safari Books Online

About this author

James Earl Douglas is a software developer focusing on functional Web APIs on the JVM. He has based many Java systems on Spring MVC and its ecosystem, which lead to authoring the e-book Barebones Spring MVC. James now researches similar and dissimilar approaches in Scala and Clojure.

Tags: Frameworks, Jackson, java, Java Web Applications, JAXB, JSON, Rich Web Applications, spring, Spring MVC, XML,

Comments are closed.