secondbreakfast
secondbreakfast

Reputation: 4374

Spring @RestController won't product application/xml

I have a @RestController with the following method

@RequestMapping(path = "/thing", method = RequestMethod.GET, 
    produces = { MediaType.APPLICATION_XML_VALUE })
public List<Thing> listThings() {
    return thingMapper.listThings();
}

But when I make a GET request with Accept:application/xml in the header, the Content-Length of the response is 0 and it doesn't produce anything. I know that data is being returned by my query and if I remove the produces attribute and make a plain get request it returns the data as json....I just cant get it to produce xml. Any ideas?

EDIT: I should mention that I am using the Spring Boot web starter

Upvotes: 2

Views: 1917

Answers (1)

Mike Adamenko
Mike Adamenko

Reputation: 3002

If you have the Jackson XML extension (jackson-dataformat-xml) on the classpath, it will be used to render XML responses and the very same example as we used for JSON would work. To use it, add the following dependency to your project:

<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
</dependency>

See here

Upvotes: 4

Related Questions