如何用自定义名称覆盖@XmlRootElement?
How to override @XmlRootElement with custom name?
当我尝试使用 @org.springframework.web.bind.annotation.RestController
中基于 'javax.xml.bind.annotation' 的 class 时,我 没有 看到 name
属性显示在响应中。我在我的 RestController 中同时使用 @org.springframework.web.bind.annotation.RequestBody
和 @org.springframework.web.bind.annotation.ResponseBody
。
例如,class Foo
的注释为 @javax.xml.bind.annotation.XmlRootElement(name="foo")
。
我得到的是 <Foo>
而不是 <foo>
。后者是我想要达到的目标。
有人可以帮我吗?
TIA。
如果您还不走运,请尝试使用 Jackson XML 注释代替 JAXB 的注释:
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement
@JacksonXmlRootElement(localName="foo")
public class Foo{
...
}
你应该有这个注释可用,因为它打包在你声明已经包含在你的项目中的 jackson-dataformat-xml
依赖项上。
如果您更喜欢使用 JAXB 注释,您应该配置 spring-mvc
以使用 org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter
作为 XML 消息转换器而不是 Jackson 的消息转换器。
当我尝试使用 @org.springframework.web.bind.annotation.RestController
中基于 'javax.xml.bind.annotation' 的 class 时,我 没有 看到 name
属性显示在响应中。我在我的 RestController 中同时使用 @org.springframework.web.bind.annotation.RequestBody
和 @org.springframework.web.bind.annotation.ResponseBody
。
例如,class Foo
的注释为 @javax.xml.bind.annotation.XmlRootElement(name="foo")
。
我得到的是 <Foo>
而不是 <foo>
。后者是我想要达到的目标。
有人可以帮我吗?
TIA。
如果您还不走运,请尝试使用 Jackson XML 注释代替 JAXB 的注释:
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement
@JacksonXmlRootElement(localName="foo")
public class Foo{
...
}
你应该有这个注释可用,因为它打包在你声明已经包含在你的项目中的 jackson-dataformat-xml
依赖项上。
如果您更喜欢使用 JAXB 注释,您应该配置 spring-mvc
以使用 org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter
作为 XML 消息转换器而不是 Jackson 的消息转换器。