Exception in Spring AOP Itself not getting handled through RestControllerAdvice

早过忘川 提交于 2021-01-29 13:42:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!