Spring Jaxb2Marshaller验证不起作用

时间:2015-10-21 21:58:21

标签: java xml spring xsd

如何使用Jaxb2Marshaller启用针对架构的验证? 以下示例如果DateTime标记错误(应该与T“2015-09-09 T 16:56:39”),我不会得到异常,unmarshaller只返回带有null的模型。

        schema: <xs:element name="ExecutionTime" minOccurs="0" maxOccurs="1"
                type="xs:dateTime" />

        tag example   <ExecutionTime>2015-09-09 16:56:39</ExecutionTime>



            Jaxb2Marshaller marshaller = null;
            marshaller = new Jaxb2Marshaller();
            marshaller.setContextPath(contextPath);
            ClassPathResource schemaResource = new   ClassPathResource(classpathXSD);
            marshaller.setSchema(schemaResource);
            marshaller.setMappedClass(Entity.class);

            marshaller.unmarshal(stringSource)

1 个答案:

答案 0 :(得分:0)

..
marshaller.setValidationEventHandler(validationEvent -> processEvent(validationEvent));
..

所以我发现,如果你指定了这个,如果你的XML有关于XSD的无效数据,你实际上会开始获得验证错误事件和异常。

相关问题