# spring-boot-demo-exception-handler > 此 demo 演示了如何在Spring Boot中进行统一的异常处理,包括了两种方式的处理:第一种对常见API形式的接口进行异常处理,统一封装返回格式;第二种是对模板页面请求的异常处理,统一处理错误页面。 ## pom.xml ```xml 4.0.0 com.xkcoding spring-boot-demo-exception-handler 0.0.1-SNAPSHOT jar spring-boot-demo-exception-handler Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.0.5.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.projectlombok lombok spring-boot-demo-exception-handler org.springframework.boot spring-boot-maven-plugin ``` ## ApiResponse.java > 统一的API格式返回封装,里面涉及到的 `BaseException` 和`Status` 这两个类,具体代码见 demo。 ```java /** *

* 通用的 API 接口封装 *

* * @package: com.xkcoding.exception.handler.model * @description: 通用的 API 接口封装 * @author: yangkai.shen * @date: Created in 2018/10/2 8:57 PM * @copyright: Copyright (c) 2018 * @version: V1.0 * @modified: yangkai.shen */ @Data public class ApiResponse { /** * 状态码 */ private Integer code; /** * 返回内容 */ private String message; /** * 返回数据 */ private Object data; /** * 无参构造函数 */ private ApiResponse() { } /** * 全参构造函数 * * @param code 状态码 * @param message 返回内容 * @param data 返回数据 */ private ApiResponse(Integer code, String message, Object data) { this.code = code; this.message = message; this.data = data; } /** * 构造一个自定义的API返回 * * @param code 状态码 * @param message 返回内容 * @param data 返回数据 * @return ApiResponse */ public static ApiResponse of(Integer code, String message, Object data) { return new ApiResponse(code, message, data); } /** * 构造一个成功且带数据的API返回 * * @param data 返回数据 * @return ApiResponse */ public static ApiResponse ofSuccess(Object data) { return ofStatus(Status.OK, data); } /** * 构造一个成功且自定义消息的API返回 * * @param message 返回内容 * @return ApiResponse */ public static ApiResponse ofMessage(String message) { return of(Status.OK.getCode(), message, null); } /** * 构造一个有状态的API返回 * * @param status 状态 {@link Status} * @return ApiResponse */ public static ApiResponse ofStatus(Status status) { return ofStatus(status, null); } /** * 构造一个有状态且带数据的API返回 * * @param status 状态 {@link Status} * @param data 返回数据 * @return ApiResponse */ public static ApiResponse ofStatus(Status status, Object data) { return of(status.getCode(), status.getMessage(), data); } /** * 构造一个异常且带数据的API返回 * * @param t 异常 * @param data 返回数据 * @param {@link BaseException} 的子类 * @return ApiResponse */ public static ApiResponse ofException(T t, Object data) { return of(t.getCode(), t.getMessage(), data); } /** * 构造一个异常且带数据的API返回 * * @param t 异常 * @param {@link BaseException} 的子类 * @return ApiResponse */ public static ApiResponse ofException(T t) { return ofException(t, null); } } ``` ## DemoExceptionHandler.java ```java /** *

* 统一异常处理 *

* * @package: com.xkcoding.exception.handler.handler * @description: 统一异常处理 * @author: yangkai.shen * @date: Created in 2018/10/2 9:26 PM * @copyright: Copyright (c) 2018 * @version: V1.0 * @modified: yangkai.shen */ @ControllerAdvice @Slf4j public class DemoExceptionHandler { private static final String DEFAULT_ERROR_VIEW = "error"; /** * 统一 json 异常处理 * * @param exception JsonException * @return 统一返回 json 格式 */ @ExceptionHandler(value = JsonException.class) @ResponseBody public ApiResponse jsonErrorHandler(JsonException exception) { log.error("【JsonException】:{}", exception.getMessage()); return ApiResponse.ofException(exception); } /** * 统一 页面 异常处理 * * @param exception PageException * @return 统一跳转到异常页面 */ @ExceptionHandler(value = PageException.class) public ModelAndView pageErrorHandler(PageException exception) { log.error("【DemoPageException】:{}", exception.getMessage()); ModelAndView view = new ModelAndView(); view.addObject("message", exception.getMessage()); view.setViewName(DEFAULT_ERROR_VIEW); return view; } } ``` ## error.html > 位于 `src/main/resources/template` 目录下 ```html 统一页面异常处理

统一页面异常处理

```