ControllerAdvice不处理AfterThrowing方面引发的异常

时间:2018-08-21 11:41:33

标签: spring spring-boot aop aspectj

此问题包括3种情况

  1. 进行HTTP调用的服务
  2. 当抛出异常时,建议上面的服务
  3. Spring boot的@ControllerAdvice,它在将结果返回给客户端之前设置状态代码

我想要实现的目标:

  1. 当服务发出返回404的HTTP调用时,我想用我的方面进行处理
  2. 我的方面应该看到服务收到404,然后抛出自定义的NotFoundException
  3. 然后,ControllerAdvice应该通过向用户返回404状态代码来处理此自定义异常。

发生的情况是,当Aspect抛出新的NotFound异常时,ControllerAdvice被跳过,并且客户端收到的是500而不是404

代码如下:

服务

@CatchHttpResult
public Map<String, Object> getUserInfo(String email) {
    cpLocation.entering("getUserInfo");
    String url = contactUrl+"/api/getContactInfo?id="+email;
    HttpEntity<?> entity = new HttpEntity<>(headers);
    ResponseEntity<Map> respEntity = restTemplate.exchange(url, HttpMethod.POST, entity,
            Map.class);
    return respEntity.getBody();
}

方面

@Aspect
@Component
public class CatchHttpResultAspect {
@AfterThrowing(pointcut = "@annotation(CatchHttpResult)", throwing = "e")
    public void httpAfterThrowing(JoinPoint joinPoint, Throwable e) throws Throwable {
        if (e instanceof HttpClientErrorException) {
            if (((HttpClientErrorException) e).getStatusCode() == HttpStatus.NOT_FOUND) {
                throw new EntityNotFoundException("The requested entity was not found.");
            }
        }
        throw e;
    }
}

ControllerAdvice

@EnableWebMvc
@ControllerAdvice
public class ExceptionsController {
    @ExceptionHandler(EntityNotFoundException.class)
    protected ResponseEntity<Object> handleEntityNotFound(EntityNotFoundException ex) {
        ApiError apiError = new ApiError(HttpStatus.NOT_FOUND);
        apiError.setMessage(ex.getMessage());
        return buildResponseEntity(apiError);
    }
}

我希望ControllerAdvice能够处理EntityNotFoundException,而不是根本找不到代码。

为什么会发生这种情况,以及如何获得所需的行为?

Stacktrace:

java.lang.reflect.UndeclaredThrowableException: null
    at po.repository.service.MicroserviceIntercomm$$EnhancerBySpringCGLIB$$d580a067.getUserInfo(<generated>)
    at po.repository.service.POService.getUserInfo(POService.java:523)
    at po.repository.controller.RepositoryController.initiateData(RepositoryController.java:47)
    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:205)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)

谢谢:)

0 个答案:

没有答案