gpt4 book ai didi

java - 处理 XMLfile XStream API 时出现 ConversionException

转载 作者:行者123 更新时间:2023-11-29 09:27:37 28 4
gpt4 key购买 nike

我在使用 OVAL 验证框架处理 XML 文件时遇到异常。我正在使用 OVAL 验证框架进行验证。我遵循了网站上记录的用户指南。 http://oval.sourceforge.net/userguide.html

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is com.thoughtworks.xstream.converters.ConversionException: 
---- Debugging information ----
cause-exception : java.lang.RuntimeException
cause-message : null
class : java.util.regex.Pattern
required-type : java.util.regex.Pattern
converter-type : com.thoughtworks.xstream.converters.extended.RegexPatternConverter
path : /oval/class/field/matchPattern/pattern
line number : 13
class[1] : net.sf.oval.constraint.MatchPatternCheck
converter-type[1] : net.sf.oval.configuration.xml.XMLConfigurer$1
class[2] : net.sf.oval.configuration.pojo.elements.FieldConfiguration
class[3] : net.sf.oval.configuration.pojo.elements.ClassConfiguration
class[4] : net.sf.oval.configuration.pojo.POJOConfigurer
version : 1.4.9
-------------------------------
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause

com.thoughtworks.xstream.converters.ConversionException:
---- Debugging information ----
cause-exception : java.lang.RuntimeException
cause-message : null
class : java.util.regex.Pattern
required-type : java.util.regex.Pattern
converter-type : com.thoughtworks.xstream.converters.extended.RegexPatternConverter
path : /oval/class/field/matchPattern/pattern
line number : 13
class[1] : net.sf.oval.constraint.MatchPatternCheck
converter-type[1] : net.sf.oval.configuration.xml.XMLConfigurer$1
class[2] : net.sf.oval.configuration.pojo.elements.FieldConfiguration
class[3] : net.sf.oval.configuration.pojo.elements.ClassConfiguration
class[4] : net.sf.oval.configuration.pojo.POJOConfigurer
version : 1.4.9
-------------------------------
com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:79)
com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:70)
com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)

我的 XML 是

<?xml version="1.0" ?>
<oval xmlns="http://oval.sf.net/oval-configuration" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://oval.sf.net/oval-configuration http://oval.sourceforge.net/oval-configuration.xsd">
<!-- define checks for the acme.model.User class -->
<!-- overwrite=false means already defined checks for this class will not
be removed -->
<class type="org.rbac.form.ConfigurationLoginForm" overwrite="false">
<field name="password">
<notNull message="password.can.not.be null"></notNull>
<notEmpty message="password.can.not.be.empty"></notEmpty>
<length min="4" max="20" message="password.length.must.be.between.min.max"></length>
<matchPattern matchAll="true">
<pattern pattern="^[a-z0-9]{8}$" flags="0" />
</matchPattern>
</field>

</class>
</oval>

这是根据 xml 验证对象的方法

XMLConfigurer xmlConfigurer = new XMLConfigurer(FormValidator.class.getResourceAsStream("/validation/login_configuration.xml"));
Guard guard = new Guard(xmlConfigurer);
Validator validator = new Validator(guard.getConfigurers());
ResourceBundleMessageResolver messageResolver = new ResourceBundleMessageResolver();
messageResolver.addMessageBundle(resourceBundle);
Validator.setMessageResolver(messageResolver);
Validator.setContextRenderer(new ResourceBundleValidationContextRenderer());
List<ConstraintViolation> LS = validator.validate(formObject);
return LS;

如果我删除模式,那么它工作正常。任何建议。

最佳答案

我在使用 1.4.10 时也遇到了类似的问题。 (OP好像用的是1.4.9)

遵循 http://oval.sourceforge.net/changes-report.html#a1.84 的变更日志中提到的版本 1.4.5但不幸的是收到了一个不同的问题。 (ArrayIndexOutOfBoundsException)

不是最佳解决方案 - 但在我更改为 1.4.1 后,验证看起来对我的情况来说工作正常。

<!-- https://mvnrepository.com/artifact/net.sf.oval/oval -->
<dependency>
<groupId>net.sf.oval</groupId>
<artifactId>oval</artifactId>
<version>1.90</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.thoughtworks.xstream/xstream -->
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.1</version>
</dependency>

我的用例如下,供引用:

<class type="com.yyy.xxx.Author">
<field name="name">
<notNull></notNull>
<notEmpty></notEmpty>
<length max="255"></length>
</field>
<field name="email">
<notNull></notNull>
<notEmpty></notEmpty>
<matchPattern matchAll="true">
<pattern pattern="^[a-zA-Z0-9.!#$%&amp;’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$"></pattern>
</matchPattern>
<length max="255"></length>
</field>
<field name="ctryCd">
<notNull></notNull>
<notEmpty></notEmpty>
<length max="3"></length>
</field>
</class>

OP 大约晚了一年......但希望它能帮助其他有类似问题的人。

关于java - 处理 XMLfile XStream API 时出现 ConversionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46012633/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com