Spring Boot 自定义错误页

首先在控制器中创建异常的请求 

@Controller

public class HelloWorldController {

  @RequestMapping("/hello")
  public String hello()
  {
    int i = 1/0;
    return  hello;
  }
}

一、简单配置

   1. 在 resource->static下创建 error文件夹并创建404.html 和500.html

   2 或在 resource->templates下创建 error文件夹并创建4xx.html 和5xx.html(优先级高于1)

二、自定义Error数据

1.自定义Error错误   

@Component
public class CustomErrorAttributes extends DefaultErrorAttributes {
    @Override
    public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
        Map<String, Object> errorAttributes = super.getErrorAttributes(webRequest,includeStackTrace);
        errorAttributes.put("timestamp", new Date());
        errorAttributes.put("custommsg","出错了");
//        errorAttributes.remove("error");
        return errorAttributes;
    }
}

2.修改4xx.html 和5xx.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<table border="1">
    <tr>
        <td>timestamp</td>
        <td th:text="${timestamp}"></td>
    </tr>
    <tr>
        <td>status</td>
        <td th:text="${status}"></td>
    </tr>
    <tr>
        <td>error</td>
        <td th:text="${error}"></td>
    </tr>
    <tr>
        <td>custommsg</td>
        <td th:text="${custommsg}"></td>
    </tr>
    <tr>
        <td>message</td>
        <td th:text="${messages}"></td>
    </tr>
    <tr>
        <td>path</td>
        <td th:text="${path}"></td>
    </tr>
</table>
</body>
</html>

三、自定义Error视图

1.定义视图配置

@Component
public class CustomErrorViewResolver implements ErrorViewResolver {
    @Override
    public   ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus status, Map<String, Object> model){
        ModelAndView mv = new ModelAndView("errorPage");
        mv.addObject("custommsg","出现错误");
        mv.addAllObjects(model);
        return mv;
    }
}

2.在 resource->templates下创建 errorPage.html  代码和4xx.html中一致

四、完全自定义

1.创建自动化配置类

@RestController
public class CustomErrorController extends BasicErrorController {

    @Autowired
    public  CustomErrorController(ErrorAttributes errorAttributes, ServerProperties serverProperties, List<ErrorViewResolver> errorViewResolverList)
    {
        super(errorAttributes,serverProperties.getError(),errorViewResolverList);
    }

    @Override
    @ResponseBody
    public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
        HttpStatus status = this.getStatus(request);
        Map<String, Object> model = getErrorAttributes(request,isIncludeStackTrace(request,MediaType.TEXT_HTML));
        model.put("custommsg","访问出错了");
        ModelAndView modelAndView = new ModelAndView("customErrorPage",model,status);
        return modelAndView ;
    }

    @Override
    public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
        Map<String,Object>  body = getErrorAttributes(request,isIncludeStackTrace(request,MediaType.TEXT_HTML));
        body.put("custommsg","访问出错了");
        HttpStatus status = getStatus(request);
        return  new ResponseEntity<>(body,status);

    }
}

2.在 resource->templates下创建 customErrorPage.html 

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>CustomErrorPage</h1>
<table border="1">
    <tr>
        <td>timestamp</td>
        <td th:text="${timestamp}"></td>
    </tr>
    <tr>
        <td>status</td>
        <td th:text="${status}"></td>
    </tr>
    <tr>
        <td>error</td>
        <td th:text="${error}"></td>
    </tr>
    <tr>
        <td>custommsg</td>
        <td th:text="${custommsg}"></td>
    </tr>
    <tr>
        <td>message</td>
        <td th:text="${messages}"></td>
    </tr>
    <tr>
        <td>path</td>
        <td th:text="${path}"></td>
    </tr>
</table>
</body>
</html>

3.页面访问

4.Postman 测试

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值