Java JAXB解组不适用于包含集合的复杂对象
我正在尝试解组一个从jersey JAXB注释类创建的XML文档Java JAXB解组不适用于包含集合的复杂对象,java,jaxb,Java,Jaxb,我正在尝试解组一个从jersey JAXB注释类创建的XML文档 JAXBContext jaxbCtx = JAXBContext.newInstance(MyClass.class); Unmarshaller m = jaxbCtx.createUnmarshaller(); MyClass result = (MyClass) m.unmarshal(in) MyClass看起来像: @XmlRootElement(name = "my-class") @XmlSeeAlso(Some
JAXBContext jaxbCtx = JAXBContext.newInstance(MyClass.class);
Unmarshaller m = jaxbCtx.createUnmarshaller();
MyClass result = (MyClass) m.unmarshal(in)
MyClass看起来像:
@XmlRootElement(name = "my-class")
@XmlSeeAlso(SomeOther.class)
public class MyClass {
private Collection<SomeOther> result;
private URI uri;
private String errorMsg;
@XmlElement
public String getError() {
return errorMsg;
}
@XmlElement
public Collection<SomeOther> getResult() {
return // some Set<SomeOther>;
}
@XmlAttribute
public URI getUri() {
return uri;
}
@XmlRootElement(name=“我的类”)
@XMLSeeAllow(SomeOther.class)
公共类MyClass{
私人收藏结果;
私有URI;
私有字符串errorMsg;
@XmlElement
公共字符串getError(){
返回errorMsg;
}
@XmlElement
公共集合getResult(){
返回//某个集合;
}
@XmlAttribute
公共URI getUri(){
返回uri;
}
示例XML如下所示:
<my-class uri="some uri">
<error></error>
<result>
<some other information in tags>
</result>
...
<result>
</result>
</my-class>
...
jaxb解组器返回的对象包含的所有值均为null;
有人能帮忙吗?
谢谢
Nayn这是因为您缺少set方法。如果您只提供get方法,那么JAXB将此属性视为只写属性 如果不希望添加集合方法,则可以将以下内容添加到类中:
@XmlAccessorType(XmlAccessType.FIELD)
然后对字段而不是属性进行注释。您可以通过浏览器点击此按钮查看XML来自您的资源吗?或者您是否已经验证了进入解组器的数据是否正确。请您解释一下这里的字段和属性是什么?字段是实例变量(如uri)属性是get/set组合(比如getUri()/setUri())。这很有效。多谢Doughan。它澄清了我对JAXB的理解。