spring 全局异常拦截器

@RestControllerAdvice
public class GlobalExceptionHandler {
    private static Logger logger = LoggerFactory.getLogger("error");

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Map defaultErrorHandler(final HttpServletRequest request, final Exception ex) {
        String requestURI = request.getRequestURI();
        String exceptionStackTrace = ExceptionUtils.getStackTrace(ex);
        logger.error("uri: {}, ex: {}", requestURI, exceptionStackTrace);

        return ImmutableMap.of("uri", requestURI, "ex", exceptionStackTrace);
    }
}

你可能感兴趣的:(基础知识,spring)