🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
下面提供4种方式来定制错误数据,最好的一种是第3种。 [TOC] # 1. SpringBoot默认的错误数据 当没有定制任何错误处理页面和数据时,访问一个不存在的地址 http://localhost:8080/abc SpringBoot根据请求的客户端不同响应不同的数据。 (1)浏览器访问,默认显示html。 ![](https://img.kancloud.cn/59/6f/596f86ddfe4e3389e38413031c7359b6_1394x308.jpg) (2)非浏览器访问,默认显示json。 ![](https://img.kancloud.cn/0d/be/0dbe4ce8bff2239e157c9f26f9fc2280_1470x384.jpg) <br/> # 2. 自定义异常处理器(一) 无论是浏览器访问,还是非浏览器方法,显示的异常数据都是JSON。 <br/> 步骤如下: **1. 自定义异常处理器** ```java import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import java.util.HashMap; import java.util.Map; @ControllerAdvice // 标注该类为异常处理器 public class MyExceptionHandler { @ResponseBody @ExceptionHandler({RuntimeException.class}) // 要处理的异常 public Map<String, Object> handlerException(Exception e) { Map<String, Object> map = new HashMap<>(); map.put("code", "我自定义的状态码500000"); map.put("message", "这是我自定义的异常消息哦!"); return map; } } ``` **2. controller层** ```java import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class IndexController { @GetMapping("/index") public String index(@RequestParam("id") int id) { if (id == 1) { throw new RuntimeException("RuntimeException!"); } return ""; } } ``` **3. 测试** 启动项目后访问一个不存在的地址 http://localhost:8080/index?id=1 ,得到如下结果。 (1)浏览器访问 ![](https://img.kancloud.cn/0f/53/0f5345dc4a7971a155825a448a1f3d77_1411x170.jpg) (2)非浏览器访问 ![](https://img.kancloud.cn/f6/15/f615386a09dcd8913c4e56c35d4a8cf8_1483x333.jpg) <br/> # 3. 自定义异常处理器(二) 浏览器访问显示错误数据显示为html,非浏览器访问显示为json。 <br/> 步骤如下: **1. 自定义异常处理器** ```java import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import javax.servlet.http.HttpServletRequest; import java.util.HashMap; import java.util.Map; @ControllerAdvice // 标注该类为异常处理器 public class MyExceptionHandler { @ExceptionHandler({RuntimeException.class}) // 要处理的异常 public String handlerException(Exception e, HttpServletRequest request) { Map<String, Object> map = new HashMap<>(); map.put("code", "我自定义的状态码500000"); map.put("message", "这是我自定义的异常消息哦!"); // 为了后面测试,请把status_code设置为500 request.setAttribute("javax.servlet.error.status_code", 500); request.setAttribute("ext", map); return "forward:/error"; // 这个error请求由SpringBoot默认提供好了的 } } ``` **2. 继承DefaultErrorAttributes** ```java import org.springframework.boot.web.servlet.error.DefaultErrorAttributes; import org.springframework.stereotype.Component; import org.springframework.web.context.request.WebRequest; import java.util.Map; @Component public class MyErrorAttributes extends DefaultErrorAttributes { @Override public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) { Map<String, Object> map = super.getErrorAttributes(webRequest, includeStackTrace); map.put("company", "发生异常了,你能拿我怎么办!"); map.put("message", "发生异常了,You get out 吧!"); Map<String, Object> ext = (Map<String, Object>) webRequest.getAttribute("ext", 0); map.put("ext", ext); return map; } } ``` **3. controller层** ```java import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class IndexController { @GetMapping("/index") public String index(@RequestParam("id") int id) { if (id == 1) { throw new RuntimeException("RuntimeException!"); } return ""; } } ``` **4. 错误模板** *`resources/template/error/5xx.html`* ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>5xx</title> </head> <body> <div>ext.Message:[[${ext.message}]]</div> <div>Message:[[${message}]]</div> <div>code:[[${ext.code}]]</div> <div>company:[[${company}]]</div> </body> </html> ``` **5. 测试** 启动项目后访问 http://localhost:8080/index?id=1 得到如下结果。 (1)浏览器访问 ![](https://img.kancloud.cn/bb/95/bb9568355f94ce10c5c7b8b23bea3fd0_1461x251.jpg) (2)非浏览器访问 ```json { "timestamp": "2021-07-20T07:50:38.412+0000", "status": 500, "error": "Internal Server Error", "message": "发生异常了,You get out 吧!", "trace": "java.lang.RuntimeException: RuntimeException!\r\n\tat com.example.boot.controller.IndexController.index(IndexController.java:13)\r\n\tat ... "path": "/index", "company": "发生异常了,你能拿我怎么办!", "ext": { "code": "我自定义的状态码500000", "message": "这是我自定义的异常消息哦!" } } ```