In a Spring MVC project we have a small use-case where we need to expose some data in XML format. We don't need an XSD for our project. Since JAXB2 is included in the JDK (up from version 6), we really did not need to configure anything: really nice!
Before, we presented a link that was followed. The XML output was automatically formatted (i.e. indented and new-lined nicely) by the browser. Now however, we needed to change the link to have the xml file automatically be downloaded. The problem here is that the file contents are not formatted.
The question is: is it possible to format the file output with minimal configuration? All examples I can find already start out by configuring view resolvers, but I hope that that is not needed. Would be elegant :)
Current setup is as follows. In the Controller:
@RequestMapping(value = "/{filename}.xml", produces = "application/xml")
public @ResponseBody XmlData downloadDataAsXml(HttpServletResponse response) {
XmlData xmlData = someLogic();
response.setHeader("Content-type", "application/octet-stream");
response.setHeader("Content-Disposition", "attachment");
return xmlData;
}
, where we use annotations on XmlData
import javax.xml.bind.annotation.*;
@XmlRootElement(name = "data")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = { "field1", "field2", "entry"})
public class XmlData {
@XmlElement private String field1;
@XmlElement private String field2;
@XmlElementWrapper(name = "entries")
@XmlElement private SortedSet<String> entry;
}
So, except for setting Spring MVC to annotation-driven, everything works out of the box but the formatting.