com.sun.istack.SAXException2:类java.util.ArrayList以及它的任何超类都是这个上下文已知的

时间:2016-03-02 09:25:47

标签: java xml rest jaxb marshalling

我正在使用XSD验证我的请求。在我的请求中,我将内容设置为List <Attribute>

属性是POJO:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "attribute", propOrder = {
    "name",
    "value"
})
public class Attribute {

    @XmlElement(required = true)
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    @XmlSchemaType(name = "NCName")
    protected String name;
    @XmlElement(required = true)
    protected Object value;

    public String getName() {
        return name;
    }
    public void setName(String value) {
        this.name = value;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }
}

我的验证失败,但有例外:

Caused by: javax.xml.bind.MarshalException
 - with linked exception:
[com.sun.istack.SAXException2: class java.util.ArrayList nor any of its super class is known to this context.

如何通过List<POJO>进行验证?

1 个答案:

答案 0 :(得分:0)

您可以为此列表创建一个包装器

 @XmlRootElement(name="attributes")
    @XmlAccessorType(XmlAccessType.Field)
    public class{
       @XmlElement(name="attributes")
       private List<Attribute> attributes;
       public void setAttributes(List<Attribute> attributes){
           this.attributes=attributes;}
       public List<Attribute> getAttributes(){
           return this.attributes;}
    }

验证将属性列表传递给此包装器的属性列表,jaxb将完成其余的工作,我们需要此类,因为解析器不接受未使用xmlRootElement和ArrayList及其所有超类注释的类没有使用xmlRootElement注释 java.util.ArrayList nor any of its super class is known to this context