How to override @XmlRootElement with custom name?

1k Views Asked by At

When I try use a 'javax.xml.bind.annotation' based class from a @org.springframework.web.bind.annotation.RestController, I don't see the value in the name attribute showing up in the response. I am using both @org.springframework.web.bind.annotation.RequestBody and @org.springframework.web.bind.annotation.ResponseBody in my RestController.

E.g., a class Foo has an annotation of @javax.xml.bind.annotation.XmlRootElement(name="foo").

What I get back is <Foo> and not <foo>. The latter is what I am trying to achieve.

Can someone please help me ?

TIA.

1

There are 1 best solutions below

0
On BEST ANSWER

If you had no luck yet, try using Jackson XML annotations instead of JAXB's ones:

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement

@JacksonXmlRootElement(localName="foo")
public class Foo{
...
}

You should have this annotation available as it is packaged on jackson-dataformat-xml dependency which you stated already had included in your project.

If you prefer to use JAXB annotations, you should configure spring-mvc to use org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter as XML message converter instead of Jackson's one.