一, 异常分类
- 异常可以处理则为 CheckedException
- 对于异常来说是无能为力的则为 RuntimeException
- 想要异常结果也显示为统一的返回结果对象,并且统一处理系统的异常信息
- 需要统一异常处理
二, 新建一个异常处理类
package com.ceshiren.springtest.core;
import com.ceshiren.springtest.exception.ServiceException;
import com.ceshiren.springtest.util.Result;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
//ControllerAdvice AOP面向切面编程,对原来对功能没有侵入性,原来什么样子现在还是什么样子
//我只是在原来的功能的基础上给你扩展出一个功能,统一异常处理功能
//@ControllerAdvice
@RestControllerAdvice
public class GlobalExceptionHandler {
String tips = "系统繁忙,请稍后重试";
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler({Exception.class})
// @ResponseBody
public Result exceptionHandler(Exception e){
//记录日志
//通知运维
//通知开发
//控制台打印异常,万一出现异常调试
e.printStackTrace();
return Result.error().message("非业务异常 "+ tips);
}
//自定义异常
@ExceptionHandler({ServiceException.class})
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public Result servceExceptionHandler(HttpServletRequest req, ServiceException se){
String requestUrl = req.getRequestURI();
String method = req.getMethod();
se.printStackTrace();
return Result.error().message("业务异常 "+ tips +" RestAPI:"+method + " "+ requestUrl).code(se.getCode());
}
//// .ArithmeticException
@ExceptionHandler({ArithmeticException.class})
// @ResponseBody
public Result exceptionHandler1(ArithmeticException e){
e.printStackTrace();
return Result.error().message("当前人数过多,请稍后再试");
}
}
三,自定义异常类
package com.ceshiren.springtest.exception;
public final class ServiceException extends RuntimeException{
/**
* 业务错误码
*/
private Integer code;
/**
* 错误提示
*/
private String message;
public ServiceException(String message) {
this.message = message;
}
public ServiceException( String message, Integer code) {
this.code = code;
this.message = message;
}
public Integer getCode() {
return code;
}
public ServiceException setCode(Integer code) {
this.code = code;
return this;
}
public String getMessage() {
return message;
}
public ServiceException setMessage(String message) {
this.message = message;
return this;
}
}
四,模拟手动抛出异常
@GetMapping("/first/api")
@ApiOperation("first-api接口")
Result firstApi(){
// System.out.println(60 /0);
throw new ServiceException("get请求异常",3000);
}