我正在记录httprequest / httpresponse信息。到mongodb, 但出现错误为:
java.lang.RuntimeException: java.lang.IllegalStateException: org.springframework.cglib.core.CodeGenerationException: java.lang.NoClassDefFoundError-->IllegalName: com.tengyun.slot.logging.filter.ApiLoggingFilter$$Lambda$870/627071892_Accessor_bc1pwb
at org.springframework.data.mapping.model.ClassGeneratingPropertyAccessorFactory.createAccessorClass(ClassGeneratingPropertyAccessorFactory.java:200)
at org.springframework.data.mapping.model.ClassGeneratingPropertyAccessorFactory.potentiallyCreateAndRegisterPersistentPropertyAccessorClass(ClassGeneratingPropertyAccessorFactory.java:184)
at org.springframework.data.mapping.model.ClassGeneratingPropertyAccessorFactory.getPropertyAccessor(ClassGeneratingPropertyAccessorFactory.java:92)
at org.springframework.data.mapping.model.BasicPersistentEntity.getPropertyAccessor(BasicPersistentEntity.java:455)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:511)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writePropertyInternal(MappingMongoConverter.java:628)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeProperties(MappingMongoConverter.java:548)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:524)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:497)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeMapInternal(MappingMongoConverter.java:779)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.createMap(MappingMongoConverter.java:691)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writePropertyInternal(MappingMongoConverter.java:580)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeProperties(MappingMongoConverter.java:548)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:524)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:497)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.write(MappingMongoConverter.java:441)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.write(MappingMongoConverter.java:78)
at org.springframework.data.mongodb.core.EntityOperations$MappedEntity.toMappedDocument(EntityOperations.java:509)
at org.springframework.data.mongodb.core.MongoTemplate.doSave(MongoTemplate.java:1398)
at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:1352)
at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.save(SimpleMongoRepository.java:84)
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.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:359)
at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:644)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:608)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$invoke$3(RepositoryFactorySupport.java:595)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:595)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy104.save(Unknown Source)
at com.tengyun.slot.logging.ApiLoggingService.saveApiLog(ApiLoggingService.java:16)
at com.tengyun.slot.logging.ApiLoggingService$$FastClassBySpringCGLIB$$2ef0d944.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.interceptor.AsyncExecutionInterceptor.lambda$invoke$0(AsyncExecutionInterceptor.java:115)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalStateException: org.springframework.cglib.core.CodeGenerationException: java.lang.NoClassDefFoundError-->IllegalName: com.tengyun.slot.logging.filter.ApiLoggingFilter$$Lambda$870/627071892_Accessor_bc1pwb
at org.springframework.data.mapping.model.ClassGeneratingPropertyAccessorFactory$PropertyAccessorClassGenerator.generateCustomAccessorClass(ClassGeneratingPropertyAccessorFactory.java:326)
at org.springframework.data.mapping.model.ClassGeneratingPropertyAccessorFactory.createAccessorClass(ClassGeneratingPropertyAccessorFactory.java:198)
... 48 common frames omitted
Caused by: org.springframework.cglib.core.CodeGenerationException: java.lang.NoClassDefFoundError-->IllegalName: com.tengyun.slot.logging.filter.ApiLoggingFilter$$Lambda$870/627071892_Accessor_bc1pwb
at org.springframework.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:526)
at org.springframework.data.mapping.model.ClassGeneratingPropertyAccessorFactory$PropertyAccessorClassGenerator.generateCustomAccessorClass(ClassGeneratingPropertyAccessorFactory.java:324)
... 49 common frames omitted
Caused by: java.lang.NoClassDefFoundError: IllegalName: com.tengyun.slot.logging.filter.ApiLoggingFilter$$Lambda$870/627071892_Accessor_bc1pwb
at java.lang.ClassLoader.preDefineClass(ClassLoader.java:654)
at java.lang.ClassLoader.defineClass(ClassLoader.java:761)
at sun.reflect.GeneratedMethodAccessor29.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:523)
... 50 common frames omitted
我的代码如下:
@Component
@Order(Integer.MIN_VALUE)
@ConfigurationProperties(prefix = "logging")
public class ApiLoggingFilter extends GenericFilterBean {
private static final Logger LOGGER = LoggerFactory.getLogger(ApiLoggingFilter.class);
@Autowired
private ObjectMapper objectMapper;
@Autowired
private ApiLoggingService apiLoggingService;
private List<String> include = new ArrayList<>();
public List<String> getInclude() {
return include;
}
public void setInclude(List<String> include) {
this.include = include;
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
ApiLogContext.clear();
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
LoggingRequestWrapper requestWrapper = new LoggingRequestWrapper(httpServletRequest);
ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(httpServletResponse);
if (!include.contains(httpServletRequest.getRequestURI())) {
chain.doFilter(request, response);
} else {
try {
ofNullable(ApiLogContext.getLog()).ifPresent(apiLog -> {
String txId = UUID.randomUUID().toString().replaceAll("-", "");
apiLog.setId(txId);
//do request properties to set in apiLog
});
chain.doFilter(requestWrapper, responseWrapper);
} finally {
ofNullable(ApiLogContext.getLog()).ifPresent(apiLog -> {
try {
requestWrapper.resetInputStream();
String requestBody = IOUtils.toString(requestWrapper.getReader());
String responseBody = IOUtils.toString(responseWrapper.getContentInputStream(), request.getCharacterEncoding());
if (!requestBody.isEmpty()) {
// request body to set in apiLog
}
if (!responseBody.isEmpty()) {
// response body to set in apiLog
}
} catch (IOException e) {
LOGGER.error("error on processing api log", e);
} finally {
try {
apiLoggingService.saveApiLog(apiLog);
} catch (Throwable t) {
t.printStackTrace();
}
}
});
responseWrapper.copyBodyToResponse();
ApiLogContext.clear();
}
}
}
}
和ApiLoggingService为:
@Service
public class ApiLoggingService {
@Autowired
private ApiLogRepository apiLogRepository;
@Async
public ApiLog saveApiLog(ApiLog apiLog) {
return apiLogRepository.save(apiLog);
}
}
,然后通过AOP将任务名称和时间设置为:
@Component
@Aspect
public class ApiLogAspect {
@Around("execution(* com.xxx.xxx.api..*.*(..))")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
MethodSignature methodSignature = (MethodSignature)pjp.getStaticPart().getSignature();
Method method = methodSignature.getMethod();
StringBuffer sb = new StringBuffer(pjp.getTarget().getClass().getCanonicalName());
String classAndMethodName = sb.append("#").append(method.getName()).toString();
long startTime = System.currentTimeMillis();
try {
return pjp.proceed();
} finally {
long taskTime = System.currentTimeMillis() - startTime;
ApiLogContext.getTask().add(new ServiceExecutionInfo(classAndMethodName, taskTime));
}
}
}
最终环境为:
我正在尝试保存一些日志,但是尽管已成功连接到mongodb,但似乎没有保存。