Spring MVC project with XML output – (pre-post)

This is a pre-post to the complete project which I’ve been working on. The complete project is a very simple Spring MVC project which records temperatures and outputs the data on a graph. I just couldn’t wait to post this though, as quite frankly, it’s cool and saves sooo much time creating XStream objects.

Instead of creating loads of XStream objects to represent your xml in/output, Spring MVC with Java 1.6 does all the work for you with just a few simple steps.

In your servlet.xml file, E.g. temperature-servlet.xml, add a namespace for mvc and add the mvc:annotation-driven tag E.g.

<?xml version="1.0" encoding="UTF-8" ?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:oxm="http://www.springframework.org/schema/oxm"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
			http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd
			http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
	
	...
	
	<mvc:annotation-driven/>
	
	...
	
</beans>

In your controller all you need to do is create a method which returns the object you would like as the root element of your XML output, in your controller interface, E.g. in this case I’m returning a Temperatures object (which will be shown separately):

public interface TemperatureController
{
	...

	@RequestMapping(value = "/temperatures.xml")
	Temperatures temperatures();
}

In the controller implementation, the code looks like the following. I have retrieved a list of Temperature objects and added them to the Temperatures (not very original name) object:

@Controller
public class TemperatureControllerImpl extends AbstractController implements TemperatureController
{
	@Autowired
	private TemperatureService temperatureService;
	
	...

	/**
	 * {@inheritDoc}
	 */
	@ResponseBody
	public Temperatures temperatures()
	{
		final Temperatures temperatures = new Temperatures();

		invokeServiceMethod(new ServiceWrapper()
		{
			@Override
			public void executeMethod()
			{
				temperatures.getTemperature().addAll(temperatureService.getAllTemperatures());
			}
		});

		return temperatures;
	}
}

The Temperatures object is the root object being sent back to the consumer, so in order for Spring to be able to output it, the object needs a Jaxb @XmlRootElement annotation at the top of the class, E.g.

@XmlRootElement
public class Temperatures
{
	private List<Temperature> temperature;

	public void setTemperature(List<Temperature> temperature)
	{
		this.temperature = temperature;
	}

	public List<Temperature> getTemperature()
	{
		if ( temperature == null )
		{
			temperature = new ArrayList<Temperature>();
		}
		return temperature;
	}
}

Of course you need a little more than the above code to get a working project, but those are the key parts which allow Spring to output your xml when you call up a url, for example, in my above example, when requesting the url temperatures.xml.

Pretty cool really! And would have saved a good number of days on one project.