自定义异常
EmpNotFoundException.java
package top.djosimon.exception; /** * @author dj * @date 2020/12/18 */ public class EmpNotFoundException extends RuntimeException { public EmpNotFoundException() { super("消息:用户未找到异常"); } }
定义控制器类
编写控制器类,处理请求,将异常抛出去。
HelloController.java
package top.djosimon.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import top.djosimon.exception.EmpNotFoundException; /** * @author dj * @date 2020/12/15 */ @Controller public class HelloController { @RequestMapping(value = "/hello") @ResponseBody public String useThymeleaf(@RequestParam("name") String name) { if ("abc".equals(name)) { throw new EmpNotFoundException(); } return "hello, " + name; } }
准备查看结果的响应页面
500.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>500</title> </head> <body> <h1>500</h1> <p>timestamp:[[${timestamp}]]</p> <p>status:[[${status}]]</p> <p>error:[[${error}]]</p> <p>exception:[[${exception}]]</p> <p>message:[[${message}]]</p> <p>errors:[[${errors}]]</p> <p>more_info:[[${more.info}]]</p> <p>more_msg:[[${more.msg}]]</p> </body> </html>
- timestamp:————时间戳
- status:————状态码
- error:————错误提示
- exception:————异常对象
- message:————异常消息,错误消息
- errors:————JSR303数据校验等错误都在这里
全局异常处理类
- 类加注解:@ControllerAdvice
- 方法加注解:@ExceptionHandler
MyExceptionHandler.java
package top.djosimon.controller; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import top.djosimon.exception.EmpNotFoundException; import javax.servlet.http.HttpServletRequest; import java.util.HashMap; import java.util.Map; /** * @author dj * @date 2020/12/18 */ // 一个全局处理异常的类 @ControllerAdvice public class MyExceptionHandler { // 没有自适应效果 /*@ExceptionHandler(EmpNotFoundException.class) @ResponseBody public Map<String, Object> empNotExist(Exception e) { Map<String, Object> map = new HashMap(); map.put("info", "user.not-fount"); map.put("msg", e.getMessage()); return map; }*/ // 自适应、自定义 @ExceptionHandler(EmpNotFoundException.class) public String empNotExist(Exception e, HttpServletRequest request) { Map<String, Object> more = new HashMap<>(); more.put("info", "info--->user!!*****not!!*****found!!"); more.put("msg", "msg--->" + e.getMessage()); // 转发到/error request.setAttribute("javax.servlet.error.status_code", 500); request.setAttribute("more", more); return "forward:/error"; } }
准备定义错误信息的组件
OneErrorAttributes.java
package top.djosimon.component; import org.springframework.boot.web.error.ErrorAttributeOptions; import org.springframework.boot.web.servlet.error.DefaultErrorAttributes; import org.springframework.web.context.request.WebRequest; import java.util.Map; /** * @author dj * @date 2020/12/18 */ // 设置我们自定义的错误属性 public class OneErrorAttributes extends DefaultErrorAttributes { @Override public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) { // respMap:最终返回给客户端的map(model/body) Map<String, Object> respMap = super.getErrorAttributes(webRequest, options.isIncluded(ErrorAttributeOptions.Include.STACK_TRACE)); // int SCOPE_REQUEST = 0; Map<String, Object> more = (Map<String, Object>) webRequest.getAttribute("more", 0); respMap.put("more", more); return respMap; } }
配置类配置组件
OneWebMvcConfigurer.java
package top.djosimon.config; import org.springframework.boot.web.servlet.error.ErrorAttributes; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import top.djosimon.component.OneErrorAttributes; /** * @author dj * @date 2020/12/15 */ @Configuration //使用WebMvcConfigurer来扩展SpringMVC的功能 public class OneWebMvcConfigurer implements WebMvcConfigurer { @Bean public ErrorAttributes errorAttributes(){ return new OneErrorAttributes(); } }
最终效果
1、响应自适应,全局异常处理类处理完异常后将异常请求转发到:/error,转由SpringBoot处理,实现web端的访问得到text/html视图页面,其他客户端访问将得到JSON数据。
2、响应自定制,除去SpringBoot默认响应内容外,通过继承ErrorAttributes改变需要返回的内容。
- web端(text/html页面)

- 其他客户端(JSON数据)

SpringBoot自动配置
自动配置类:ErrorMvcAutoConfiguration
四个重要组件
DefaultErrorAttributes(默认错误属性)
帮助在页面共享信息。
BasicErrorController(基本错误控制器)
处理默认的/error请求的两种方式(以text/html 或者 json数据进行返回),响应页面,去哪个页面是由DefaultErrorViewResolver解析得到的。
ErrorPageCustomizer(错误页面控制器)
一旦系统出现4xx、5xx之类的错误,ErrorPageCustomizer就会生效。
DefaultErrorViewResolver(默认视图解析器)
解析页面,进行显示。
0 条评论