我目前正在解决一个非常奇怪的问题。我基本上想要做的是使用Bean Validation来验证一些(非JPA)对象。然而,随着对象类的生成,我暂时坚持用xml定义验证。 由于它是参考实现,我使用了hibernate-validator,为项目添加了必要的maven依赖项,并构成了一个简单的xml验证映射和测试。
当测试执行时,我得到一个异常
javax.validation.ValidationException: HV000123: Unable to parse META-INF/validation.xml.
at org.hibernate.validator.internal.xml.XmlParserHelper.getSchemaVersion(XmlParserHelper.java:112)
...
Caused by: java.io.IOException: Stream closed
at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:156)
at java.io.BufferedInputStream.reset(BufferedInputStream.java:425)
at org.hibernate.validator.internal.xml.XmlParserHelper.getSchemaVersion(XmlParserHelper.java:109)
... 29 more
有趣的是,IOException在XmlParserHelper.getSchemaVersion(...)的finally块中抛出。这里发生了什么?这可能是导致此错误的原因?以下是有关设置的更多信息。
验证文件位于src / main / resources / META-INF(validation.xml和constraints.xml)中,如下所示:
的validation.xml:
<validation-config xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration">
<default-provider>org.hibernate.validator.HibernateValidator</default-provider>
<message-interpolator>org.hibernate.validator.engine.ResourceBundleMessageInterpolator</message-interpolator>
<traversable-resolver>org.hibernate.validator.engine.resolver.DefaultTraversableResolver</traversable-resolver>
<constraint-validator-factory>org.hibernate.validator.engine.ConstraintValidatorFactoryImpl</constraint-validator-factory>
<constraint-mapping>/constraints.xml</constraint-mapping>
</validation-config>
constraints.xml:
<constraint-mappings xmlns="http://jboss.org/xml/ns/javax/validation/mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping validation-mapping-1.1.xsd">
<default-package>com.nano</default-package>
<bean class="Person">
<field name="familyName">
<constraint annotation="javax.validation.constraints.Size">
<element name="min">4</element>
</constraint>
</field>
</bean>
</constraint-mappings>
项目中的相关maven依赖项是:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.3.Final</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
</dependency>
发生异常的测试是一个非常基本的概念验证测试,它执行以下操作:
Person p = new Person();
p.setFamilienName("x");
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation<Person>> violations = validator.validate(p);
assertEquals(1, violations);
答案 0 :(得分:3)
此错误发生在哪里(计算机设置)?在我们的一个项目中使用hibernate验证器时遇到了完全相同的问题。在本地计算机上一切正常。对于我们使用IBM AIX服务器的阶段。在具有IBM SDK Version 6的AIX服务器上,我得到了相同的异常:
Caused by: javax.validation.ValidationException: HV000123: Unable to parse META-INF/validation.xml.
at org.hibernate.validator.internal.xml.XmlParserHelper.getSchemaVersion(XmlParserHelper.java:108)
at org.hibernate.validator.internal.xml.ValidationXmlParser.parseValidationXml(ValidationXmlParser.java:76)
at org.hibernate.validator.internal.engine.ConfigurationImpl.getBootstrapConfiguration(ConfigurationImpl.java:287)
at org.hibernate.validator.internal.engine.ConfigurationImpl.parseValidationXml(ConfigurationImpl.java:361)
at org.hibernate.validator.internal.engine.ConfigurationImpl.buildValidatorFactory(ConfigurationImpl.java:214)
at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:110)
at org.hibernate.cfg.beanvalidation.TypeSafeActivator.getValidatorFactory(TypeSafeActivator.java:459)
... 29 more
Caused by: java.io.IOException: Stream closed
at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:156)
at java.io.BufferedInputStream.reset(BufferedInputStream.java:425)
at org.hibernate.validator.internal.xml.XmlParserHelper.getSchemaVersion(XmlParserHelper.java:105)
... 35 more
查看XmlParserHelper
处的代码,会发生以下情况:
javax.xml.stream.XmlInputFactory
为validation.xml文件创建javax.xml.stream.XmlEventReader
。BufferedInputStream.reset()
由于IBM使用自己的StAX 1.0实现(XL XP-J 1.1 - 请参阅http://www-01.ibm.com/support/knowledgecenter/SSYKE2_6.0.0/com.ibm.java.doc.user.aix64.60/user/xml/using_xml.html),因此这应该是特定于实现的内容。
因此我尝试更改XmlInputFactory实现以查看它是否适用于另一个实现。我能够使用Codehaus的StAX 1.2在本地重现相同的异常。此实现使用com.bea.xml.stream.MXParserFactory
来创建阅读器。如果没有其他内容可供阅读,com.bea.xml.stream.reader.XmlReader.read()
- 方法会关闭流:
/**
* Reads the number of characters read into the buffer, or -1 on EOF.
*/
public int read(char buf [], int off, int len) throws IOException
{
int val;
if (closed)
return -1; // throw new IOException ("closed");
val = in.read (buf, off, len);
if (val == -1)
close ();
return val;
}
看起来IBM SDK具有类似的行为,它会在读取时关闭流(也可能在您的环境中也有这种行为)。
解决方案:最后,我使用其他StAX实现(JAXP参考实现1.4 - 请参阅https://jaxp.java.net/)。它也与Woodstox合作。要更改应用程序中的StAX实现,您必须执行以下操作:
com.sun.xml.internal.stream.XMLInputFactoryImpl
)XmlInputFactory
中创建XmlParserHelper
时,hibernaet验证程序会使用此文件中定义的类。