java - How to format XML ResponseBody output without or with minimal configuration in SpringMVC -
in spring mvc project have small use-case need expose data in xml format. don't need xsd our project. since jaxb2 included in jdk (up version 6), did not need configure anything: nice!
before, presented link followed. xml output automatically formatted (i.e. indented , new-lined nicely) browser. however, needed change link have xml file automatically downloaded. problem here file contents not formatted.
the question is: possible format file output minimal configuration? examples can find start out configuring view resolvers, hope that not needed. elegant :)
current setup follows. in 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; }
, 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 setting spring mvc annotation-driven, works out of box formatting.
Comments
Post a Comment