当前位置: 首页 > 知识库问答 >
问题:

在验证中添加SpringWebConstraintValidatorFactory时出现StackOverflower错误。xml

诸葛雅达
2023-03-14

我正试图在custom ConstraintValidator中使用@Autowired annotation。

我已经阅读了一些解决方案,但没有一个有效,所以我试图使用Spring的org.springframework.web.bind.support.SpringWebConstraintValidatorFactory强制Hibernate,以创建自动构建的ConstraintValidator实例。

这正是文件所说的:

公共类SpringWebConstraintValidatorFactory:

JSR-303 ConstraintValidator工厂实现,它将委托给当前的Spring WebApplicationContext以创建自动连接的ConstraintValidator实例。与SpringConstraintValidatorFactory不同,此变体用于标准验证中的声明性使用。xml文件,例如与JAX-RS或JAX-WS结合使用。

所以我提供了META-INF/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" version="1.1">

    <constraint-validator-factory>
        org.springframework.web.bind.support.SpringWebConstraintValidatorFactory
    </constraint-validator-factory>

</validation-config>

但当数据经过验证时,我得到了java。StackOverflowerr先生

以下是堆栈跟踪:

org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.StackOverflowError
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:982)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    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)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:150)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilterInternal(BasicAuthenticationFilter.java:158)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214)
    org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177)
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

如果没有这个配置,应用程序也能正常工作。

所以从一开始,我有自定义约束验证器,它使用自动配置的用户服务:

public class EmailValidator implements ConstraintValidator<ValidEmail, String> {

   @Autowired
   UserService userService;

   public void initialize(ValidEmail constraint) {
   }

   public boolean isValid(String email, ConstraintValidatorContext context) {
          return userService.isEmailAvailable(email);
   }
}

奇怪的是,只有在填写完整的注册表格时,当我试图执行测试(甚至不涉及验证)时,userService无法自动连接,因此它是空的。当我只填写用户名和电子邮件时,它是OK的——它得到验证,我得到了自定义costraint中定义的消息,这意味着userService被正确调用,但当我试图用有效数据验证完整的注册表时——userService为空。就好像有时有效,有时无效。在这种情况下,当它不应该自动连接时,它是如何被验证的——至少正如我所读到的那样。

当我尝试从isValid函数中获取返回语句时,它就像预期的那样工作,即使在测试中也是如此,但是它不是werid吗?我想为此提供正常的解决方案。这就是为什么我尝试更改ConstraintValidatorFactory。但是也许有不同的方法来修复它?

我想我已经尝试了Stack的所有解决方案,但都不起作用。

我的测试类是这样配置的,但我不认为是这样的:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/appTestconfig-root.xml")
@WebAppConfiguration
public class UserDAOTest {
  ...
}

我错过了什么?

以下是我不更改验证配置时的堆栈跟踪:

javax.validation.ValidationException: HV000028: Unexpected exception during isValid call.

    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateSingleConstraint(ConstraintTree.java:449)
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:127)
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:87)
    at org.hibernate.validator.internal.metadata.core.MetaConstraint.validateConstraint(MetaConstraint.java:73)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateMetaConstraint(ValidatorImpl.java:616)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraint(ValidatorImpl.java:581)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForSingleDefaultGroupElement(ValidatorImpl.java:527)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForDefaultGroup(ValidatorImpl.java:495)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:460)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:410)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validate(ValidatorImpl.java:207)
    at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:116)
    at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:80)
    at org.hibernate.action.internal.EntityInsertAction.preInsert(EntityInsertAction.java:205)
    at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:82)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:589)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:463)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337)
    at org.hibernate.event.internal.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:50)
    at org.hibernate.internal.SessionImpl.autoFlushIfRequired(SessionImpl.java:1396)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1481)
    at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1445)
    at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1414)
    at org.hibernate.query.internal.AbstractProducedQuery.uniqueResult(AbstractProducedQuery.java:1457)
    at todolist.daos.UserDAOimpl.getUserByName(UserDAOimpl.java:25)
    at todolist.daos.UserDAOTest.addUser(UserDAOTest.java:42)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.NullPointerException
    at todolist.validators.EmailValidator.isValid(EmailValidator.java:19)
    at todolist.validators.EmailValidator.isValid(EmailValidator.java:10)
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateSingleConstraint(ConstraintTree.java:446)
    ... 54 more

共有1个答案

陆正奇
2023-03-14

哇,我解决了那个问题。

事实上,问题完全不同。

问题在于Hibernate。我不知道它会在每次插入/更新时自动验证数据。在这种情况下,@Autowired不起作用,因为Hibernate使用不同的验证器工厂实现。

我之前正在验证数据,这就是为什么当不是所有的东西都是有效的时候它都有效。当一切都是有效的时候,保存操作被调用,Hibernate重新运行验证...

所以我在配置文件hibernate中禁用了它。cfg。xml

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
     <session-factory>
         ...

         <property name="javax.persistence.validation.mode">none</property>

         ...
     </session-factory>
</hibernate-configuration>

现在一切都像预期的那样。

 类似资料:
  • 尝试将Azure AD身份验证添加到具有. net核心2.1后端的Angular 7 webapp。 然而,我在请求过程中收到了CORS错误。 “访问位于的XMLHttpRequest”https://login.microsoftonline.com/.......“(重定向自”https://localhost:5001/api/auth/login“)起源”https://localhost

  • 我在laravel 5.3中工作,我想在现有表格中添加列(即课程章节)。所以我已经通过命令创建了迁移文件 迁移文件已经创建,我已经添加了一些代码来在该表中添加列,如下所示 我执行命令后 PHP工匠迁移 表没有被创建,而不是我得到了错误 任何答案? 我现有的表迁移文件是

  • 我想从我的对象生成一个JSON字符串: 每次我尝试这样做,我得到这个错误: 这些是我的BomItem类的属性: 我引用的BomModule类的属性: 知道是什么导致了这个错误吗?我该如何修复它?

  • 问题内容: 我想在用户输入错误的同时在“视图”页面中显示验证错误。可以的是,当用户输入错误时,它不会在数据库中保存任何内容。但是用户视图页面中没有错误消息。如果有人发现错误,请帮助我。 这是控制器: 这是路线: 这是刀片视图页面: 这是错误消息块: 问题答案: 如果您使用的是5.2.27或更高版本,请尝试删除中间件。现在,Laravel会自动将中间件应用于内部的所有路由,如果您尝试手动添加它,则会

  • 我试图在我的rails应用程序中使用LinkedIn的api进行身份验证。我对rails很陌生,所以我遵循了以下指南http://sourcey.com/rails-4-omniauth-using-devise-with-twitter-facebook-and-linkedin/ 耙中止!NameError:main的未定义局部变量或方法'/users/AlexanderKehaya/.rvm

  • 我正在创建我的第一个angular Electronic项目,这里是我的错误日志,我正在尝试添加新的字体和样式在我的angular 4项目 我已经从我的项目中删除了bootstrap,因为style.css包含所有bootstrap代码和我个人的css multi/node_modules/microfinanceassets/css/style中出错。未找到CSS模块:错误:无法解析“/home