diff --git a/examples/boot-api/src/main/java/org/springside/examples/bootapi/api/support/CustomExceptionHandler.java b/examples/boot-api/src/main/java/org/springside/examples/bootapi/api/support/CustomExceptionHandler.java index 15f05450b..6217c3da8 100644 --- a/examples/boot-api/src/main/java/org/springside/examples/bootapi/api/support/CustomExceptionHandler.java +++ b/examples/boot-api/src/main/java/org/springside/examples/bootapi/api/support/CustomExceptionHandler.java @@ -77,7 +77,7 @@ public void logError(Exception ex, HttpServletRequest request) { map.put("message", ex.getMessage()); map.put("from", request.getRemoteAddr()); String queryString = request.getQueryString(); - map.put("uri", queryString != null ? (request.getRequestURI() + "?" + queryString) : request.getRequestURI()); + map.put("path", queryString != null ? (request.getRequestURI() + "?" + queryString) : request.getRequestURI()); logger.error(jsonMapper.toJson(map), ex); } diff --git a/examples/boot-api/src/main/java/org/springside/examples/bootapi/api/support/ErrorPageController.java b/examples/boot-api/src/main/java/org/springside/examples/bootapi/api/support/ErrorPageController.java index 00763c1b5..80185139f 100644 --- a/examples/boot-api/src/main/java/org/springside/examples/bootapi/api/support/ErrorPageController.java +++ b/examples/boot-api/src/main/java/org/springside/examples/bootapi/api/support/ErrorPageController.java @@ -7,68 +7,60 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.web.DefaultErrorAttributes; +import org.springframework.boot.autoconfigure.web.ErrorAttributes; import org.springframework.boot.autoconfigure.web.ErrorController; -import org.springframework.http.HttpStatus; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.context.request.RequestAttributes; +import org.springframework.web.context.request.ServletRequestAttributes; import org.springside.modules.mapper.JsonMapper; import org.springside.modules.web.MediaTypes; -import com.google.common.collect.Maps; - /** - * 重载替换Spring Boot默认的BasicErrorController + * 重载替换Spring Boot默认的BasicErrorController, 增加日志并让错误返回方式统一. * * @author calvin - * */ @Controller public class ErrorPageController implements ErrorController { - private Logger logger = LoggerFactory.getLogger(ErrorPageController.class); - - private JsonMapper jsonMapper = new JsonMapper(); + private static Logger logger = LoggerFactory.getLogger(ErrorPageController.class); @Value("${error.path:/error}") private String errorPath; - @Override - public String getErrorPath() { - return this.errorPath; - } + private JsonMapper jsonMapper = new JsonMapper(); + + private ErrorAttributes errorAttributes = new DefaultErrorAttributes(); @RequestMapping(value = "${error.path:/error}", produces = MediaTypes.JSON_UTF_8) @ResponseBody public ErrorResult handle(HttpServletRequest request) { + Map attributes = getErrorAttributes(request); + ErrorResult result = new ErrorResult(); - result.code = getStatus(request).value(); - result.message = (String) request.getAttribute("javax.servlet.error.message"); + result.code = (int) attributes.get("status"); + result.message = (String) attributes.get("error"); - logError(result, request); + logError(attributes, request); return result; } - private HttpStatus getStatus(HttpServletRequest request) { - Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code"); - if (statusCode != null) { - try { - return HttpStatus.valueOf(statusCode); - } catch (Exception ex) { - } - } - return HttpStatus.INTERNAL_SERVER_ERROR; + private Map getErrorAttributes(HttpServletRequest request) { + RequestAttributes requestAttributes = new ServletRequestAttributes(request); + return this.errorAttributes.getErrorAttributes(requestAttributes, false); } - private void logError(ErrorResult result, HttpServletRequest request) { - Map map = Maps.newHashMap(); - map.put("code", result.code); - map.put("message", result.message); - map.put("from", request.getRemoteAddr()); - String queryString = request.getQueryString(); - map.put("uri", queryString != null ? (request.getRequestURI() + "?" + queryString) : request.getRequestURI()); + private void logError(Map attributes, HttpServletRequest request) { + attributes.put("from", request.getRemoteAddr()); + logger.error(jsonMapper.toJson(attributes)); + } - logger.error(jsonMapper.toJson(map)); + @Override + public String getErrorPath() { + return this.errorPath; } }