Loading

SpringBoot 自定义异常处理

SpringBoot 自定义异常处理

1.自定义异常数据

HelloController.java

package com.zhuantai.exception.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ANTIA1
 * @date 2021/7/8 10:52
 */
@RestController
public class HelloController {

    @GetMapping("/hello")
    public Integer hello(){
        return 1/0;
    }
}

500.html

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

MyErrorAttributes.java

package com.zhuantai.exception;

import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.WebRequest;

import java.util.Map;

/**
 * 自定义异常数据
 * @author ANTIA1
 * @date 2021/7/8 10:41
 */
@Component
public class MyErrorAttributes extends DefaultErrorAttributes {
    //自定义异常数据
    @Override
    public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
        Map<String, Object> map = super.getErrorAttributes(webRequest, options);
        if ((Integer)map.get("status") == 500){
            map.put("message","服务器错误");
        }
        return map;
    }
}

运行结果:

image-20210708110755504

2.自定义异常视图

999.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    
    <title>999</title>
</head>
<body>
<h1>999.html</h1>
<table border="1">
    <tr>
        <td>path</td>
        <td th:text="${path}"></td>
    </tr>
    <tr>
        <td>error</td>
        <td th:text="${error}"></td>
    </tr>
    <tr>
        <td>message</td>
        <td th:text="${message}"></td>
    </tr>
    <tr>
        <td>timestamp</td>
        <td th:text="${timestamp}"></td>
    </tr>
    <tr>
        <td>status</td>
        <td th:text="${status}"></td>
    </tr>
</table>
</body>
</html>

MyErrorViewResolver.java

package com.zhuantai.exception;

import org.springframework.boot.autoconfigure.web.ResourceProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.DefaultErrorViewResolver;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 自定义异常视图
 * @author ANTIA1
 * @date 2021/7/8 10:46
 */
@Component
public class MyErrorViewResolver extends DefaultErrorViewResolver {
    public MyErrorViewResolver(ApplicationContext applicationContext, ResourceProperties resourceProperties) {
        super(applicationContext, resourceProperties);
    }

    @Override
    public ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus status, Map<String, Object> model) {
        //定义异常视图
        ModelAndView mv = new ModelAndView("zhuantai/999",model);

        //一般情况下建议不在这里定义异常数据,@model 这里的map是一个不可修改的map
        //但还是有解决方案的
        Map<String,Object> map = new HashMap<>();
        map.putAll(map);
        if ((Integer)map.get("status") == 500){
            map.put("message","服务器内部错误");
        }
        return mv;
    }
}

运行结果:

image-20210708110952642

posted @ 2021-08-02 23:13  ANTIA11  阅读(184)  评论(0编辑  收藏  举报