Exception

@ControllerAdvice 에서 HttpRequest 파라미터를 받지 못합니다.

Jungsoomin :) 2020. 12. 16. 10:34
  • HttpServletRequest, WebRequest 를 쓰자 .. 
  • 에러가 자주 남, o.s.b:spring-boot-starter-web 넣고 HttpServletRequest를 쓰자.
@RestControllerAdvice
@Slf4j
public class GlobalControllerExceptionHandler {

    @ResponseStatus(NOT_FOUND)
    @ExceptionHandler(NotFoundException.class)
    public @ResponseBody HttpErrorInfo handleNotFoundExceptions(HttpServletRequest request, Exception ex) {
        return createHttpErrorInfo(NOT_FOUND,request,ex);
    }

    private HttpErrorInfo createHttpErrorInfo(HttpStatus httpStatus, HttpServletRequest request, Exception ex) {
        final String path = request.getRequestURI();
        final String message = ex.getMessage();

        log.debug("Returning HTTP Status: {} for path: {}, message: {}",httpStatus,path,message);
        return new HttpErrorInfo(path,httpStatus,message);
    }
}