spring boot全局异常处理

@ControllerAdvice
@Slf4j
public class ServiceExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    Object handleException(Exception exception, HttpServletRequest request) {
        exception.printStackTrace();
        log.error(exception.getMessage(), exception);
        MDC.put("RequestURI", request.getRequestURI());
        request.getParameterMap().forEach((k, v) -> MDC.put(k, Arrays.toString(v)));
        return new ResultBean<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), "服务器骄傲了 !", exception.getMessage());
    }

    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    Object handleIllegalArgumentException(IllegalArgumentException exception, HttpServletRequest request) {
        log.error(exception.getMessage(), exception);
        MDC.put("RequestURI", request.getRequestURI());
        request.getParameterMap().forEach((k, v) -> MDC.put(k, Arrays.toString(v)));
        return new ResultBean<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), exception.getMessage(), null);
    }
}

你可能感兴趣的:(spring boot全局异常处理)