项目为了实现角色校验在controller方法加了个RoleControl 注解. 使用@Before进行切面。在测试过程中发现业务层抛出的自定义异常没有被@ControllerAdvice 捕获到。 网上有些说是需要自定义异常继承RuntimeException, 但我的自定义异常确实继承了RuntimeException。 Debug进行跟踪发现再经过切面之后抛出了一个java.lang.reflect.UndeclaredThrowableException, 然后
进入了
@ExceptionHandler(Exception.class) @ResponseStatus(value = HttpStatus.OK) @ResponseBody public Object handleException(HttpServletRequest request, Exception ex) { ex.printStackTrace(); return HttpJsonResult.error("系统异常,请联系管理员"); }
反复对比使用@Before的代码发现
MethodInvocation invocation = (MethodInvocation) BeanTpl.forceGetProperty(joinPoint, "methodInvocation"); Method mdItf = invocation.getMethod(); Method mdImpl = joinPoint.getTarget().getClass().getDeclaredMethod(mdItf.getName(), mdItf.getParameterTypes()); 。。。。 // mdItf.invoke(joinPoint.getTarget(), args); 就是这段代码引起的,注释掉之后就好了。