Spring AOP 拦截Controller 参数中MultipartFile file 反射getMethod报错问题

阿坤同桌
  • 11

问题描述

在Controller 有一个web接口如下
@ApiImplicitParams({
        @ApiImplicitParam(name = "file")
})
@ApiOperation(value = "人脸识别")
@PostMapping("/checkPic")
public Result checkPic(@RequestParam("file") MultipartFile file) {
    String token = UserInterceptor.getToken(request);
    HxUser userInfo = hxService.getUserInfo(token);

    // 业务代码...
    return Result.ok();
}

在Spring AOP 拦截中,有一段代码是通过目标类获取调用的方法信息,获取@Token标记注解

 Method method = targetClazz.getMethod(point.getSignature().getName(), argTypes);

有了上面一个场景就可以很好的来阐述问题了.先来看报错的堆栈信息

java.lang.reflect.UndeclaredThrowableException: null
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:767)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:95)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:692)
    at cn.akwangl.controller.hx.HxWebController$$EnhancerBySpringCGLIB$$72891801.checkPic(<generated>)
    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.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.springframework.web.filter.CorsFilter.doFilterInternal(CorsFilter.java:92)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1707)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NoSuchMethodException: cn.akwangl.controller.hx.HxWebController.checkPic(org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile)
    at java.lang.Class.getMethod(Class.java:1786)
    at cn.akwangl.aop.UserInterceptor.isCheck(UserInterceptor.java:82)
    at cn.akwangl.aop.UserInterceptor.doAround(UserInterceptor.java:44)
    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.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:644)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:633)
    at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:175)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750)
    ... 59 common frames omitted

其实比较重点的报错信息是Caused by: java.lang.NoSuchMethodException: cn.akwangl.controller.hx.HxWebController.checkPic(org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile)它在告诉你找不到对应的方法.为什么会找不到呢?那是应为在Controller中具体的参数类型是org.springframework.web.multipart.MultipartFile而在AOP拦截的切面则是org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile代理类,所以在运行 Method method = targetClazz.getMethod(point.getSignature().getName(), argTypes);会报此错误

解决

我看了一眼org.springframework.web.multipart.support.StandardMultipartHttpServletRequest发现有如下方法
image.png
而这里的getMultiFileMap方法返回的是Map类型,这就和我们在Controller中用注解@RequestParam("file") MultipartFile file绑定就很类似,所以要解决以上问题使其AOP拦截生效只需要把Controller参数改为org.springframework.web.multipart.support.StandardMultipartHttpServletRequest类型.然后再通过public方法获取自己的参数.跟注解获取没什么区别,只不过一个是手动一个是自动.其底层调用都是org.springframework.web.multipart.support.StandardMultipartHttpServletRequest类.

@ApiImplicitParams({
        @ApiImplicitParam(name = "file")
})
@ApiOperation(value = "人脸识别")
@PostMapping("/checkPic")
public Result checkPic(StandardMultipartHttpServletRequest standardMultipartRequest) {
    // 这个key 可能为NULL 需要自己判断
    MultipartFile file = standardMultipartRequest.getFile("file");
    String token = UserInterceptor.getToken(request);
    HxUser userInfo = hxService.getUserInfo(token);

    // 业务代码...
    return Result.ok();
}

如果你还有更好的解决方式请告诉我.

回复
阅读 433
1 个回答
    MethodSignature signature = (MethodSignature) point.getSignature();
    Method method = signature.getMethod();
你知道吗?

宣传栏