Mythul
Mythul

Reputation: 1807

How to return a XML file with Spring MVC?

I have this method:

@RequestMapping( value = DetailedMetaDataController.RESOURCE_PATH + ".xml", headers = "Accept=application/json" )
public void exportXml( @RequestBody JSONObject json ) throws IOException
{
    String responseString = JacksonUtils.toXmlAsString();
}

The responseString is a XML String. The AJAX request is a post because I send large amount of data, but the AJAX always returns error.

I need a simple Window with which to save the String as a xml file.

Upvotes: 0

Views: 1588

Answers (2)

Mateusz
Mateusz

Reputation: 3048

You can wrap the data in a bean annotated with @javax.xml.bind.annotation.XmlRootElement, field with @javax.xml.bind.annotation.XmlElement and change method signature from public void to public @org.springframework.web.bind.annotation.ResponseBody BeanClassName, returning corresponding BeanClassName instance. Also remember about <mvc:annotation-driven /> in your Spring XML config file.

For more info see this.

Upvotes: 1

PaulProgrammer
PaulProgrammer

Reputation: 17720

Annotate with @Produces(MediaType.APPLICATION_XML) and make sure your request uses application/xml as part of an Accept header.

Upvotes: 2

Related Questions