I have a spring-boot 1.1.7 application that uses Thymeleaf for much of the UI, so the response from my controllers hasn\'t really been a concern. However, now I need to pro
I am not sure about your version of Spring Boot (1.1.7.RELEASE) but I am on version 1.5.2.RELEASE and this xml conversion / serialization happens automatically without usage of any jackson dependencies as mentioned in few of the answers.
I guess that is happening because org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter
is automatically configured since Spring Boot version 1.5.1.RELEASE & that converter uses default JAXB implementation of JRE ( so no explicit xml conversion dependency needed ) .
Second, Accept
header set by clients in request decides which format the output is expected so a request mapping like below ( i.e. a single end point ) ,
@RequestMapping(method = RequestMethod.GET, value = "/remote/search", produces = {
MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE, MediaType.TEXT_XML_VALUE })
can be used to produce an xml as well as a JSON response ( if Accept header is set as text/xml
or application/xml
& application/json
respectively.
Note 1 : javax.xml.bind.annotation.XmlRootElement
needs to be specified on root class if xml response is expected for a Java class. This is mandatory.
Note 2 : Jackson for json is already included in Spring Boot so that is not to be explicitly included for json outputs
Note 3 : Accept header - Output match off happens automatically by framework & developer doesn't have to code anything specific for that.
So in my opinion, if you only add XmlRootElement
to your base class & upgrade your Spring Boot version, your server side is all set. Responsibility to set correct Accept header lies with the clients.
You've marked the controller method as producing application/xml
responses (produces = MediaType.APPLICATION_XML_VALUE
). The request's accept header (Accept: text/xml
) doesn't match so Spring determines that your search
method cannot handle the request.
There are a few different ways to fix this on the server, depending on your exact requirements:
produces
attribute entirelyproduces = { "application/xml", "text/xml" }