全局异常处理
1.1 前置准备
/**
* @author muzi@softeem.com
* @description 常量
* @since 2022/1/13 20:13
*/
public interface Constants {
/**
* 返回码
*/
interface Code{
/**
* 成功的code
*/
int SUCCESS = 10000;
/**
* 密码错误code
*/
int PASSWORD_FAILURE = 20000;
/**
* 爱你是个错误code
*/
int LOVEU_FAILURE = 30000;
/**
* 未知异常
*/
int OTHER_FAILURE = 99999;
}
/**
* 返回码信息
*/
interface ErrorMSG{
String SUCCESS_MSG = "接口调用成功";
String PASSWORD_FAILURE_MSG = "密码错误";
String LOVEU_FAILURE_MSG = "爱你是个错误";
String OTHER_FAILURE_MSG = "未知异常";
}
}
/**
* @author muzi@softeem.com
* @description 自定义异常的父类
* @since 2022/1/13 20:19
*/
public class BaseException extends RuntimeException{
/**
* 错误码
*/
private int code;
/**
* 错误信息
*/
private String msg;
public BaseException() {
}
public BaseException(int code, String msg) {
super();
this.code = code;
this.msg = msg;
}
//省略getter/setter
}
public class LOVEUException extends BaseException {
public LOVEUException() {
super(Constants.Code.LOVEU_FAILURE, Constants.ErrorMSG.LOVEU_FAILURE_MSG);
}
}
/**
* @author muzi@softeem.com
* @description 密码错误异常
* @since 2022/1/13 20:21
*/
public class PasswordException extends BaseException{
public PasswordException() {
super(Constants.Code.PASSWORD_FAILURE,Constants.ErrorMSG.PASSWORD_FAILURE_MSG);
// super(20000, "密码错误");
}
}
1.2 返回数据的全局异常处理
@RestController
@RequestMapping("myex")
public class MyExceptionController {
@GetMapping("checkpwd")
public Map<String,Object> checkPassword() {
Map<String, Object> map = new HashMap<>();
if (1 == 1) {
// throw new PasswordException();
// throw new LOVEUException();
throw new RuntimeException();
}
map.put("code",Constants.Code.SUCCESS);
map.put("msg", Constants.ErrorMSG.SUCCESS_MSG);
return map;
}
}
@RestControllerAdvice(annotations = RestController.class)
public class RestControllerExceptionHandler {
/**
* 代表在被@RestController注解修饰的类中,抛出了BaseException,则有该方法来处理
* @param e
* @return
*/
@ExceptionHandler(BaseException.class)
public Map<String, Object> baseExceptionHandler(BaseException e) {
Map<String, Object> map = new HashMap<>();
map.put("code", e.getCode());
map.put("msg", e.getMsg());
return map;
}
@ExceptionHandler(Exception.class)
public Map<String, Object> otherExceptionHandler(Exception e) {
Map<String, Object> map = new HashMap<>();
map.put("code", Code.OTHER_FAILURE);
map.put("msg", ErrorMSG.OTHER_FAILURE_MSG);
return map;
}
}
1.3 跳转页面的全局异常处
base-error
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>BaseException返回的页面</title>
</head>
<body>
错误码:<span th:text="${code}"></span>
错误信息:<span th:text="${msg}"></span>
</body>
</html>
other-error
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>通用的Exception返回的页面</title>
</head>
<body>
错误码:<span th:text="${code}"></span>
错误信息:<span th:text="${msg}"></span>
</body>
</html>
@Controller
@RequestMapping("myex")
public class MyExceptionController {
@GetMapping("checkpwd")
public ModelAndView checkPassword() {
ModelAndView mav = new ModelAndView("mythyme");
if (1 == 1) {
// throw new PasswordException();
throw new LOVEUException();
// throw new RuntimeException();
}
List<Student> studentList = new ArrayList();
for (int i = 0; i < 5; i++) {
Student student = new Student();
student.setName("木子" + i);
student.setAge(18+i);
student.setAddress("湖北武汉" + i);
studentList.add(student);
}
mav.addObject("studentList", studentList);
return mav;
}
}
@ControllerAdvice(annotations = Controller.class)
public class ControllerExceptionHandler {
@ExceptionHandler(BaseException.class)
public ModelAndView baseExceptionHandler(BaseException e) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("code", e.getCode());
modelAndView.addObject("msg", e.getMsg());
modelAndView.setViewName("base-error");
return modelAndView;
}
@ExceptionHandler(Exception.class)
public ModelAndView otherExceptionHandler(Exception e) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("code",Constants.Code.OTHER_FAILURE);
modelAndView.addObject("msg", Constants.ErrorMSG.OTHER_FAILURE_MSG);
modelAndView.setViewName("other-error");
return modelAndView;
}
}