Springboot2.X global exception handling in project combat

Global exception handling in SpringBoot2.x online education project

Introduction: Explain SpringBoot2.X global exception handling

Why configure global exceptions?

  • Not suitable for global server error scenario 1/0, null pointer, etc.

Configuration benefits

  • Unified error page or error code
  • more user friendly

How does Springboot2.X configure global exceptions in the project

  • Annotate the class

    • @ControllerAdvice, if you need to return json data, the method needs to add @ResponseBody
    • @RestControllerAdvice, returns json data by default, the method does not need to add @ResponseBody
  • method to add handler

    • Catch global exceptions, handle all unknowable exceptions
    • @ExceptionHandler(value=Exception.class)

controller layer

@RestController
@RequestMapping("api/v1/test")
public class TestController {
    
    
 @GetMapping("list")
    public JsonData testExt(){
    
    
        int i =1 /0;
        return JsonData.buildSuccess("");
    }
}

handle layer

/**
 * 标记这是一个异常处理
 */
@RestControllerAdvice  //标记用于处理异常
//@ControllerAdvice
public class CustomExHandle {
    
    
    //处理json
    @ExceptionHandler(value = Exception.class)
    Object handleException(Exception e, HttpServletRequest request){
    
    
       return JsonData.buildError("服务端出问题",-2);
    }
}

@RestControllerAdvice and @ControllerAdvice for handling json and non-json data


SpringBoot2.x custom global exception return page

Use SpringBoot to customize exception and error pages to jump to actual combat

To return to the custom exception interface, you need to introduce thymeleaf dependencies (not required, not necessary if it is a simple html interface)

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

Create error.html error page

html page

<!DOCTYPE html>
<!--在使用时需添加 xmlns:th="http://www/thymeleaf.org"-->
<html lang="en" xmlns:th="http://www/thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
这是一个自定义异常的界面

<!--获取到报错的信息 等,按需求获取所需的报错信息
    注:msg必须与hanlde层中自定义的一致
    e.getMessage() :报错的信息
    e.getClass()   :报错的类型
-->
<p th:text="${msg}"></p>
</body>
</html>

handle layer

/**
 * 标记这是一个异常处理
 */
//@RestControllerAdvice  //标记用于处理异常
@ControllerAdvice
public class CustomExHandle {
    
    
//    //处理json
//    @ExceptionHandler(value = Exception.class)
//    Object handleException(Exception e, HttpServletRequest request){
    
    
//       return JsonData.buildError("服务端出问题",-2);
//    }

    @ExceptionHandler(value = Exception.class)
    Object handleException(Exception e, HttpServletRequest request){
    
    
        ModelAndView modelAndView =new ModelAndView();
        modelAndView.setViewName("error.html");
        modelAndView.addObject("msg",e.getMessage());
        System.out.println(e.getMessage());
        return modelAndView;
    }
}

Guess you like

Origin blog.csdn.net/m0_49969111/article/details/118694253