0
我使用spring-web
發送POST
xml請求到web服務。但我的XML bean請求不使我jaxb
屬性中定義的方式:Spring RestTemplate忽略XML annoations(例如@XmlAttribute)?
//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 Jaxb模塊以使用jaxb註釋https://github.com/FasterXML/jackson-module-jaxb-annotations。我實際上最終從'com.fasterxml.jackson.dataformat.xml.annotation'使用'@JacksonXmlProperty(isAttribute = true)'和'@JacksonXmlRootElement(localName =「document」)'' – varren
但是我怎樣才能告訴'xsdtojava'使用'@ JacksonXmlProperty'?我寧願不想修改自動生成的類... – membersound