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 测试

发布了255 篇原创文章 · 获赞 39 · 访问量 35万+

猜你喜欢

转载自blog.csdn.net/kangguang/article/details/104267926