1

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.

qkrijger
  • 26,686
  • 6
  • 36
  • 37
  • [This](http://stackoverflow.com/questions/4882988/how-to-get-formatted-xml-output-from-jaxb-in-spring) may or may not be helpful. – GriffeyDog Apr 25 '14 at 14:20
  • @GriffeyDog, saw that. However, there is more XML configuration there than needed. I'd actually like a way to set the property `Marshaller.JAXB_FORMATTED_OUTPUT` with an annotation or something like that. I'd like not to have to configure a whole bean just for this, when everything else works so nicely out of the box. – qkrijger Apr 25 '14 at 14:24
  • That is the appropriate way to do it. – Sotirios Delimanolis Apr 25 '14 at 14:28

0 Answers0