java.lang.IllegalStateException:找不到线程绑定请求+ @JaversSpringDataAuditable

时间:2017-04-25 17:05:37

标签: java jpa spring-boot javers

当我在JPA存储库类中使用Javers Audit功能时,我得到了java.lang.IllegalStateException: No thread-bound request found

@JaversSpringDataAuditable

我的服务方法(创建/更新)由@Async定义,并使用Future处理。

Future<T> updateEmployee(Employee employee) {
    //some code here
    return new AsyncResult<T>(employeeDTO);
}

我在这里遗漏了什么吗?

  

java.util.concurrent.ExecutionException:java.lang.IllegalStateException:否       发现线程绑定请求:您是指在实际Web请求之外的请求属性,还是在最初接收线程之外处理请求?如果您实际在Web请求中操作并仍然收到此消息,则您的代码可能在DispatcherServlet / DispatcherPortlet之外运行:在这种情况下,请使用RequestContextListener或RequestContextFilter来公开当前请求。       at java.util.concurrent.FutureTask.report(Unknown Source)〜[na:1.8.0_112]       at java.util.concurrent.FutureTask.get(Unknown Source)〜[na:1.8.0_112]

     

引起:java.lang.IllegalStateException:找不到线程绑定请求:       您是指实际Web请求之外的请求属性,还是       处理原始接收线程之外的请求?如果你是       实际上在Web请求中运行并仍然收到此消息,您的       代码可能在DispatcherServlet / DispatcherPortlet之外运行:In       在这种情况下,使用RequestContextListener或RequestContextFilter来公开       当前的request.at

     

org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131)〜[spring-web-4.1.9.RELEASE.jar:4.1.9.RELEASE]               在org.springframework.web.context.support.WebApplicationContextUtils.currentRequestAttributes(WebApplicationContextUtils.java:275)〜[spring-web-4.1.9.RELEASE.jar:4.1.9.RELEASE]               在org.springframework.web.context.support.WebApplicationContextUtils.access $ 400(WebApplicationContextUtils.java:64)〜[spring-web-4.1.9.RELEASE.jar:4.1.9.RELEASE]               在org.springframework.web.context.support.WebApplicationContextUtils $ RequestObjectFactory.getObject(WebApplicationContextUtils.java:291)〜[spring-web-4.1.9.RELEASE.jar:4.1.9.RELEASE]               at org.springframework.web.context.support.WebApplicationContextUtils $ RequestObjectFactory.getObject(WebApplicationContextUtils.java:286)~ [spring-web-4.1.9.RELEASE.jar:4.1.9.RELEASE]               在org.springframework.beans.factory.support.AutowireUtils $ ObjectFactoryDe​​legatingInvocationHandler.invoke(AutowireUtils.java:307)〜[spring-beans-4.1.9.RELEASE.jar:4.1.9.RELEASE]               在com.sun.proxy。$ Proxy146.getHeader(未知来源)〜[na:na]               在com.vz.uiam.common.audit.javers.ServletRequestAuthorProvider.provide(ServletRequestAuthorProvider.java:15)〜[uiam_common_cloud-1.0.11.jar:1.0.11]               在org.javers.spring.auditable.aspect.OnSaveAuditChangeHandler.handle(OnSaveAuditChangeHandler.java:17)〜[javers-spring-1.6.4.jar:na]               在org.javers.spring.auditable.aspect.JaversAuditableRepositoryAspect.applyVersionChange(JaversAuditableRepositoryAspect.java:94)〜[javers-spring-1.6.4.jar:na]               在org.javers.spring.auditable.aspect.JaversAuditableRepositoryAspect.applyVersionChanges(JaversAuditableRepositoryAspect.java:89)〜[javers-spring-1.6.4.jar:na]               在org.javers.spring.auditable.aspect.JaversAuditableRepositoryAspect.onVersionEvent(JaversAuditableRepositoryAspect.java:71)〜[javers-spring-1.6.4.jar:na]               在org.javers.spring.auditable.aspect.JaversAuditableRepositoryAspect.onSaveExecuted(JaversAuditableRepositoryAspect.java:59)〜[javers-spring-1.6.4.jar:na]               at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)〜[na:1.8.0_112]               at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)〜[na:1.8.0_112]               at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)〜[na:1.8.0_112]               at java.lang.reflect.Method.invoke(Unknown Source)〜[na:1.8.0_112]               在org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621)[spring-aop-4.1.9.RELEASE.jar:4.1.9.RELEASE]               在org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:603)[spring-aop-4.1.9.RELEASE.jar:4.1.9.RELEASE]               在org.springframework.aop.aspectj.AspectJAfterReturningAdvice.afterReturning(AspectJAfterReturningAdvice.java:61)〜[spring-aop-4.1.9.RELEASE.jar:4.1.9.RELEASE]               在org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:53)〜[spring-aop-4.1.9.RELEASE.jar:4.1.9.RELEASE]               在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:168)[spring-aop-4.1.9.RELEASE.jar:4.1.9.RELEASE]               在org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:85)[spring-aop-4.1.9.RELEASE.jar:4.1.9.RELEASE]               在com.vz.uiam.common.audit.aspect.JPAIdFieldNullifyAspect.monitor(JPAIdFieldNullifyAspect.java:55)〜[uiam_common_cloud-1.0.11.jar:1.0.11]               at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)〜[na:1.8.0_112]               at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)〜[na:1.8.0_112]               at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)〜[na:1.8.0_112]               at java.lang.reflect.Method.invoke(Unknown Source)〜[na:1.8.0_112]               在org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621)[spring-aop-4.1.9.RELEASE.jar:4.1.9.RELEASE]               在org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610)[spring-aop-4.1.9.RELEASE.jar:4.1.9.RELEASE]               在org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:68)[spring-aop-4.1.9.RELEASE.jar:4.1.9.RELEASE]               在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:168)[spring-aop-4.1.9.RELEASE.jar:4.1.9.RELEASE]               在org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)[spring-aop-4.1.9.RELEASE.jar:4.1.9.RELEASE]               在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)[spring-aop-4.1.9.RELEASE.jar:4.1.9.RELEASE]               在org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)〜[spring-aop-4.1.9.RELEASE.jar:4.1.9.RELEASE]               在com.sun.proxy。$ Proxy149.save(未知来源)〜[na:na]

2 个答案:

答案 0 :(得分:0)

Javers正试图在当前的网络请求中找到修改的作者。但是,由于您似乎不在Web请求的上下文中,因此失败。

如果您确实不在Web请求中,则似乎需要提供另一个AuthorProvider bean而不是ServletRequestAuthorProvider(可能是默认值)。

答案 1 :(得分:0)

Nyamiou给出的答案Galeanthrope是对的,但只是为了澄清:

com.vz.uiam.common.audit.javers.ServletRequestAuthorProvider绝对不是JaVers类,而是AuthorProvider接口的一些实现。

默认情况下(如果您使用的是spring-boot-starter-data-jpa),JaVers会为您提供SpringSecurityAuthorProvider