com.sun.istack.SAXException2:类java.util.ArrayList或其任何超类对于此上下文已知
问题描述:
我使用XSD验证了我的请求。在我的要求中,我将我的内容设置为List <Attribute>
。com.sun.istack.SAXException2:类java.util.ArrayList或其任何超类对于此上下文已知
属性是一个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>
进行验证?
答
您可以创建一个包装此列表
@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名单将完成剩下的属性列表,我们需要这个类,因为解析器没有按” t接受未用xmlRootElement和ArrayList注解的类以及其所有超类未用xmlRootElement注释 java.util.ArrayList nor any of its super class is known to this context
不能验证列表元素每个项目一次 – achabahe