创建统一信息返回类
使用有参构造器返回所需要的信息
public class Resp<T> {
//服务端返回的错误码
private int code = 200;
//服务端返回的错误信息
private String msg = "success";
//我们服务端返回的数据
private T data;
private Resp(int code,String msg,T data){
this.code = code;
this.msg = msg;
this.data = data;
}
public static <T> Resp success(T data){
Resp resp = new Resp(200, "success", data);
return resp;
}
public static <T> Resp success(String msg,T data){
Resp resp = new Resp(200,msg, data);
return resp;
}
public static <T> Resp error(AppExceptionCodeMsg appExceptionCodeMsg){
Resp resp = new Resp(appExceptionCodeMsg.getCode(), appExceptionCodeMsg.getMsg(), null);
return resp;
}
public static <T> Resp error(int code,String msg){
Resp resp = new Resp(code,msg, null);
return resp;
}
public int getCode() {
return code;
}
public String getMsg() {
return msg;
}
public T getData() {
return data;
}
}
枚举类型
定于枚举类内可包含业务异常,统一异常处理回进行拦截返回对应的错误码与错误信息
public enum AppExceptionCodeMsg {
INVALID_CODE(10000,"验证码无效"),
USERNAME_NOT_EXISTS(10001,"用户名不存在"),
USER_CREDIT_NOT_ENOUTH(10002,"用户积分不足");
;
private int code ;
private String msg ;
public int getCode() {
return code;
}
public String getMsg() {
return msg;
}
AppExceptionCodeMsg(int code, String msg){
this.code = code;
this.msg = msg;
}
}
自定义异常类
public class AppException extends RuntimeException{
private int code = 500;
private String msg = "服务器异常";
public AppException(AppExceptionCodeMsg appExceptionCodeMsg){
super();
this.code = appExceptionCodeMsg.getCode();
this.msg = appExceptionCodeMsg.getMsg();
}
public AppException(int code,String msg){
super();
this.code = code;
this.msg = msg;
}
public int getCode() {
return code;
}
public String getMsg() {
return msg;
}
}
全局统一异常处理类
全局统一异常处理类首先会判断异常是否是枚举类中的异常,如果是就返回对应的状态码和状态信息,如果不是返回500,"服务器端异常"
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(value = {Exception.class})
@ResponseBody
public <T> Resp<T> exceptionHandler(Exception e){
//这里先判断拦截到的Exception是不是我们自定义的异常类型
if(e instanceof AppException){
AppException appException = (AppException)e;
return Resp.error(appException.getCode(),appException.getMsg());
}
//如果拦截的异常不是我们自定义的异常(例如:数据库主键冲突)
return Resp.error(500,"服务器端异常");
}
}
创建一个controller
调用Resp.success("")可直接返回成功的状态码以及状态信息
使用throw new AppException(AppExceptionCodeMsg.USERNAME_NOT_EXISTS);可抛出枚举类中定义的异常信息
@RestController
public class DemoController {
@GetMapping("demo")
public Resp<String> demo1(String name){
if("ok".equals(name)){
return Resp.success("succ");
}
if("err".equals(name)){
//抛业务相关的异常
throw new AppException(AppExceptionCodeMsg.USERNAME_NOT_EXISTS);
}
if("errcode".equals(name)){
throw new AppException(AppExceptionCodeMsg.INVALID_CODE);
}
if("0".equals(name)){
int i=1/0;
}
//检查用户积分是否足够,如果不够,就抛出异常
if("notenough".equals(name)){
throw new AppException(AppExceptionCodeMsg.USER_CREDIT_NOT_ENOUTH);
}
return Resp.success("default");
}
@GetMapping("list")
public Resp<List> list(){
List<String> list = Arrays.asList("zhangsan","lisi","wangwu");
return Resp.success(list);
}
}