【问题标题】:com.sun.istack.SAXException2: class java.util.ArrayList nor any of its super class is known to this contextcom.sun.istack.SAXException2:此上下文已知类 java.util.ArrayList 或其任何超类
【发布时间】:2016-03-02 09:25:47
【问题描述】:

我正在使用 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>进行验证??

【问题讨论】:

  • 你不能一次验证每个项目的列表元素

标签: java xml rest jaxb marshalling


【解决方案1】:

你可以为这个列表创建一个包装器

 @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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-16
    • 2021-05-27
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 2012-12-13
    • 2014-07-03
    • 2013-09-13
    相关资源
    最近更新 更多