问题
I need to validate the HttpServletRequest for certain attribtues in my service layer before proceeding. I created @Before advice, Please note that AOP method throws Exception. I want exception thrown by this method should be handled by RestControllerAdvice. I can see that AOP method is being executed but DataNotValidException is not being handled by RestControllerAdvice. RestConrollerAdvice is working fine if there is any exception on the validation of other parameters or any exception in RestController.
@Before("execution(* com.mycom.service.app.myappName.handler*.*.*(..)) && args(httpServletRequest,..)" )
public void validateRequest(JoinPoint joinPoint, HttpServletRequest httpServletRequest) throws DataNotValidException {
This is my @RestControllerAdvice. I tried to override handleExceptionInternal as well.
public String handleLinkage(final HttpServletRequest httpServletRequest, @Valid UserLinkRequest userLinkReqeust, final HttpHeaders httpHeaders) {
@RestControllerAdvice
@Slf4j
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(value = { InvalidFormatException.class, DataNotValidException.class, Exception.class,
SQLException.class })
private ResponseEntity<ResponseObjectData> handleAllException(Exception exception, HttpHeaders headers,
WebRequest request, HandlerMethod handlerMethod) {
// logErrorNBuildResponse
}
@Override
protected ResponseEntity<Object> handleExceptionInternal(
Exception ex, @Nullable Object body, HttpHeaders headers, HttpStatus status, WebRequest request) {
// logErrorNBuildResponse
} }
来源:https://stackoverflow.com/questions/60890319/exception-in-spring-aop-itself-not-getting-handled-through-restcontrolleradvice