2

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

secondbreakfast
  • 4,194
  • 5
  • 47
  • 101
  • You don't get xml because of @ResponseBody annotation injected with RestController. Refer https://stackoverflow.com/questions/4856298/spring-mvc-3-returning-xml-through-responsebody – harshavmb Jun 08 '17 at 14:00
  • @harshavmb care to elaborate? I'm not really following the question you posted. I added `@ResponseBody` to my method but the result is the same – secondbreakfast Jun 08 '17 at 14:20
  • 1
    Adding @ResponseBody alone will not work. You need to have some xml parser to marshal and unmarshal Java objects. Mike's answer is also more or less related to that but he has got Jackson xml parser. Clear now? – harshavmb Jun 08 '17 at 14:24

1 Answers1

4

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

Mike Adamenko
  • 2,944
  • 1
  • 15
  • 28
  • This made it work, and I don't even need the `produces` attribute or `@ResponseBody` annotation. However, I feel as though the Spring Boot web starter should be fully equipped to handle xml responses without an additional dependency so I'm going to hold off on marking this as the accepted answer for now – secondbreakfast Jun 08 '17 at 14:22
  • I mean, unless you want to save me the time and explain why this dependency is actually needed and that spring boot web starter isnt capable of doing it without it? – secondbreakfast Jun 08 '17 at 14:24
  • 1
    You don't need '@ResponseBody' as '@RestController' has that. – harshavmb Jun 08 '17 at 14:27
  • straight from the horses mouth, can't argue with that. I wonder why they made the decision to not provide xml REST functionality out of the box with the Spring Boot web starter, I appreciate both of your insights – secondbreakfast Jun 08 '17 at 14:29
  • 1
    Reason is simple they have Jackson json parser with @ResponseBody annotation. Jackson is the default json parser comes with OOTB spring-rest module. It's mentioned in the link I shared. Glad to know that issue is resolved.. – harshavmb Jun 08 '17 at 14:30
  • 1
    BTW, I found a bug on SO. Will flag! – harshavmb Jun 08 '17 at 14:32