此问题包括3种情况
我想要实现的目标:
发生的情况是,当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;
}
}
@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)
谢谢:)