Spring Boot 自定义错误响应

SpringBoot自定义错误响应

目录

SpringBoot自定义错误响应

1.默认错误响应

2.自定义响应页面

3.错误页面参数传递

3.1 默认参数

3.2 自定义参数传递

4.总结


1.默认错误响应


使用SpringBoot默认错误响应效果如下:

默认错误响应效果

响应的html源码如下

<html>
     <body>
         <h1>Whitelabel Error Page</h1>
         <p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p>
         <div id='created'>Thu Feb 27 19:29:37 CST 2020</div>
         <div>There was an unexpected error (type=Bad Request, status=400).</div>                        
         <div>no user named xiaohong</div>
     </body>
 </html>
 

2.自定义响应页面


在模板引擎目录下新建文件error/4xx.html

文件目录
 编辑4xx.html
<!DOCTYPE html>
 <html lang="en">
 <head>
     <meta charset="UTF-8">
     <title>error</title>
 </head>
 <body>
 <h1>亲,页面找不到了!</h1>
 ​
 </body>
 </html>

在浏览器中访问一个错误地址,效果如下

自定义错误响应效果

说明:可以为不同响应码建立对应的html文件,命名方式为响应码.html,例如400.html,404.html,没有这些则找4xx.html

3.错误页面参数传递


3.1 默认参数

SpringBoot 默认可以获取的参数有以下几个

 timestamp:时间戳
 status:状态码
 error:错误提示
 exception:异常对象
 message:异常消息
 errors:JSR303数据校验的错误都在这里

重新编辑4xx.html

 <!DOCTYPE html>
 <html lang="en" xmlns:th="http://www.thymeleaf.org">
 <head>
     <meta charset="UTF-8">
     <title>error</title>
 </head>
 <body>
      <h1>亲,页面找不到了!</h1>
     <h1>timestamp:[[${timestamp}]]</h1>
     <h1>status:[[${status}]]</h1>
     <h1>error:[[${error}]]</h1>
     <h1>path:[[${path}]]</h1>
 </body>
 </html>

浏览器响应结果

使用默认参数的错误响应

3.2 自定义参数传递

有时候我们需要向错误页面传递一些自定义参数,这种情况下需要进行以下几个步骤

  1. 编写异常控制器类,并传递参数

  2. 继承DefaultErrorAttributes类,复写getErrorAttributes()方法

  3. 在响应页面中使用参数

代码如下

自定义测试Exception

public class UserNotExists extends RuntimeException {
     public UserNotExists() {
         this("no such user");
     }
 ​
     public UserNotExists(String message) {
         super(message);
     }
 }

编写异常控制器类

 @ControllerAdvice
 public class MyErrorController {
 ​
     @ExceptionHandler(UserNotExists.class)//传入需要处理的异常类
     public String handlerUserNotException(Exception e, HttpServletRequest request){
         Map<String,Object> map=new HashMap<>();
         map.put("code","UserNotExists");
         map.put("myMessage",e.getMessage());
         request.setAttribute("extMap",map);//将传递的参数加入到request中
         request.setAttribute("javax.servlet.error.status_code",400);//这里一定要设置响应码,要不然会走向错误的处理流程
         return "forward:/error";
     }
 }

继承DefaultErrorAttributes类,复写getErrorAttributes()方法

@Component
 public class MyExceptionHandler extends DefaultErrorAttributes {
     @Override
     public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
         Map<String, Object> errorAttributes = super.getErrorAttributes(webRequest, includeStackTrace);//父类会返回系统默认的参数Map
         Map<String,Object> extMap = (Map<String, Object>) webRequest.getAttribute("extMap", 0);//0代表从request中取出参数
 ​
         Set<String> keySet = extMap.keySet();
         //遍历传递过来的map,并将值加入到父类返回的参数map中
         for (String s : keySet) {
             errorAttributes.put(s,extMap.get(s));
         }
 ​
         return errorAttributes;
     }
 }

重新编写4xx.html

 <!DOCTYPE html>
 <html lang="en" xmlns:th="http://www.thymeleaf.org">
 <head>
     <meta charset="UTF-8">
     <title>error</title>
 </head>
 <body>
  <h1>亲,页面找不到了!</h1>
 <h1>timestamp:[[${timestamp}]]</h1>
  <h1>status:[[${status}]]</h1>
  <h1>error:[[${error}]]</h1>
  <h1>path:[[${path}]]</h1>
  <h1>code:[[${code}]]</h1> <!--自定义的参数code-->
  <h1>myMessage:[[${myMessage}]]</h1><!--自定义的参数 myMessage-->
 </body>
 </html>

浏览器响应结果

使用了自定义参数的错误响应

4.总结

(1)使用上面这种方式可以利用SpringBoot为不同客户端请求发送不同类型数据的特性。例如:浏览器请求发送html页面,其他客户端发送json数据;

(2)参考资料

发布了5 篇原创文章 · 获赞 4 · 访问量 234

猜你喜欢

转载自blog.csdn.net/qq_26192391/article/details/104543704
今日推荐