【发布时间】:2018-02-23 15:54:29
【问题描述】:
我正在使用spring-web 将POST xml 请求发送到网络服务。但是我的 Xml bean 请求并没有按照我使用 jaxb 属性定义的方式呈现:
//this class is autogenerated with xsdtojava from *.xsd files
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "myRequest")
public class ServletRequest {
@XmlAttribute(name = "age")
private int age;
}
用法:
ServletRequest req = new ServletRequest();
req.setAge(20);
restTemplateBuilder.build().postForEntity(url, new HttpEntity<>(req), MyRsp.class);
发送的结果 xml:
<ServletRequest>
<age>20</age>
</ServletRequest>
问题:@XmlRootElement 名称和 @XmlAttribute 注释似乎都被忽略了。为什么?
【问题讨论】:
-
我很久以前就遇到过类似的问题,我认为这更多是关于 Jackson 设置而不是 Spring。您可能已经这样做了,您必须注册 Jackson Jaxb 模块才能使用 jaxb 注释 github.com/FasterXML/jackson-module-jaxb-annotations。我实际上最终使用了来自
com.fasterxml.jackson.dataformat.xml.annotation的@JacksonXmlProperty(isAttribute = true)和@JacksonXmlRootElement(localName = "document") -
但是我如何告诉
xsdtojava使用@JacksonXmlProperty?我不想修改自动生成的类...
标签: java spring spring-mvc jaxb jackson