SpringBoot2.0 base case (03): Configure global mapping system exception handling

First, the malfunction classification

Here's abnormal classification system from the perspective of abnormal processing of view, the main classification categories: business exceptions and system exceptions.

1, business exceptions

Business disorders are some of the predictable exception handling service faults, prompting the user to operate and improve the operability of the system.
Common business exceptions tips:
1) Enter xxx
2) can not be empty xxx
xxx repeat 3), replace

2, system abnormalities

The system disorders are some of the unpredictability of an exception, an exception handling system, allows to show a user-friendly interface, easy to cause resentment to the user. If it is a financial system, the emergence of a system crash abnormal interface in the user interface, the user is likely to lead directly to the loss.
Common system abnormalities tips:
1) page is missing 404
2) abnormal server 500

Second, the solution after the application starts 404 interface

1, the introduction page Jar package

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

2, Custom Home Interface

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexController {
    @RequestMapping("/")
    public String index(ModelMap modelMap) {
        modelMap.addAttribute("name","知了一笑") ;
        return "index";
    }
}

3 Home interface

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8" />
    <title></title>
</head>
<body>
<h1 th:text="${name}"></h1>
</body>
</html>

4, operating results

Three, SpringBoot2.0 exception handling

1, program structure.

2, the custom business exception class

public class ServiceException extends Exception {
    public ServiceException (String msg){
        super(msg);
    }
}

3, describe the object custom exception

public class ReturnException {
    // 响应码
    private Integer code;
    // 异常描述
    private String msg;
    // 请求的Url
    private String url;
    // 省略 get set 方法
}

4, exception handling unified format

1) two annotations based
@ControllerAdvice defined uniform exception handling class
@ExceptionHandler defined corresponding to the type of exception processing mode
2) code to achieve

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
@ControllerAdvice
// 异常以Json格式返回 等同 ExceptionHandler + ResponseBody 注解
// @RestControllerAdvice
public class HandlerException {
    /**
     * 自定义业务异常映射,返回JSON格式提示
     */
    @ExceptionHandler(value = ServiceException.class)
    @ResponseBody
    public ReturnException handler01 (HttpServletRequest request,ServiceException e){
        ReturnException returnException = new ReturnException() ;
        returnException.setCode(600);
        returnException.setMsg(e.getMessage());
        returnException.setUrl(String.valueOf(request.getRequestURL()));
        return returnException ;
    }
    /**
     * 服务异常
     */
    @ExceptionHandler(value = Exception.class)
    public ModelAndView handler02 (HttpServletRequest request,Exception e){
        ModelAndView modelAndView = new ModelAndView() ;
        modelAndView.addObject("ExeMsg", e.getMessage());
        modelAndView.addObject("ReqUrl", request.getRequestURL());
        modelAndView.setViewName("/exemsg");
        return modelAndView ;
    }
}

5 simple test interface

@Controller
public class ExeController {
    /**
     *  {
     *    "code": 600,
     *    "msg": "业务异常:ID 不能为空",
     *    "url": "http://localhost:8003/exception01"
     *  }
     */
    @RequestMapping("/exception01")
    public String exception01 () throws ServiceException {
        throw new ServiceException("业务异常:ID 不能为空");
    }

    @RequestMapping("/exception02")
    public String exception02 () throws Exception {
        throw new Exception("出现异常,全体卧倒");
    }
}

Fourth, the source address

GitHub:知了一笑
https://github.com/cicadasmile/spring-boot-base


Guess you like

Origin www.cnblogs.com/cicada-smile/p/10994293.html