通过具有StringBuffer属性的JAXB封送对象时,该属性将变为空白.我编写了一个小程序来演示该问题:
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class JaxbTest {
private String valueOne;
private StringBuffer valueTwo;
public static void main(String[] args) throws Exception {
JaxbTest object = new JaxbTest();
object.setValueOne("12345");
object.setValueTwo(new StringBuffer("54321"));
JAXBContext context = JAXBContext.newInstance(JaxbTest.class);
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(object, System.out);
}
@XmlElement
public String getValueOne() {
return valueOne;
}
public void setValueOne(String valueOne) {
this.valueOne = valueOne;
}
@XmlElement
public StringBuffer getValueTwo() {
return valueTwo;
}
public void setValueTwo(StringBuffer valueTwo) {
this.valueTwo = valueTwo;
}
}
输出如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>12345
有谁知道为什么“ valueTwo”没有正确封送?顺便说一句,我正在使用Java 1.6.0_22.
提前致谢!!!
解决方法:
对于这种用例,我建议使用JAXB的XmlAdapter:
标签:jaxb,java
来源: https://codeday.me/bug/20191023/1913359.html