使用DynamicJAXBContext进行验证

时间:2015-08-06 11:42:45

标签: jaxb moxy

如何在解组时启用验证? [moxy 2.6.0]

首先有趣的是我需要放置应该已经存在于DynamicContext中的模式。但即使将模式设置为unmarshaller,它也会给我一个错误。

我试图使用这个:

 final JAXBUnmarshaller unmarshaller = dynamicJAXBContext.createUnmarshaller();
        //unmarshaller.setSchema();
        final Schema schema = schemas.get(type);
        unmarshaller.setSchema(schema);
        unmarshaller.setValidating(true);

它给出了一个例外。看起来打破这个的部分是从DynamicContext

放置的一些元素
Exception Description: An error occurred resolving the XML Schema.
Internal Exception: java.lang.NullPointerException
Exception [EclipseLink-25012] (Eclipse Persistence Services - 2.6.0.v20150309-bf26070): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: An error occurred resolving the XML Schema.
Internal Exception: java.lang.NullPointerException
    at org.eclipse.persistence.exceptions.XMLMarshalException.errorResolvingXMLSchema(XMLMarshalException.java:186)
    at org.eclipse.persistence.oxm.schema.XMLSchemaClassPathReference.getURL(XMLSchemaClassPathReference.java:48)
    at org.eclipse.persistence.oxm.XMLUnmarshaller.initializeSchemas(XMLUnmarshaller.java:211)
    at org.eclipse.persistence.oxm.XMLUnmarshaller.setValidationMode(XMLUnmarshaller.java:155)
    at org.eclipse.persistence.jaxb.JAXBUnmarshaller.setValidating(JAXBUnmarshaller.java:756)

1 个答案:

答案 0 :(得分:3)

这里的问题是找不到架构。您设置null而不是Schema实例。

顺便说一下。方法

javax.xml.bind.Unmarshaller#setValidating
自JAXB 2.0以来,

已弃用。使用方法

javax.xml.bind.Unmarshaller#setSchema

将完成这项工作。