返回结果实体类
package entity;
import java.io.Serializable;
public class Result implements Serializable {
private boolean flag;//是否成功
private Integer code;// 返回码
private String message;//返回信息
private Object data;// 返回数据
public Result(boolean flag, Integer code, String message, Object data) {
super();
this.flag = flag;
this.code = code;
this.message = message;
this.data = data;
}
public Result() {
}
public Result(boolean flag, Integer code, String message) {
super();
this.flag = flag;
this.code = code;
this.message = message;
}
public static Result error(String message){
return new Result(false, StatusCode.ERROR, message, null);
}
public static Result success(String message){
return new Result(true, StatusCode.OK, message, null);
}
public static Result success(String message,Object obj){
return new Result(true, StatusCode.OK, message, obj);
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
自定义异常
/**
* 自定义异常:终止已知不符合业务逻辑的操作的继续执行
*/
public class MyException extends RuntimeException {
public MyException(String message){
super(message);
}
}
全局异常处理
import entity.Result;
import exception.MyException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.List;
// 切的是controller
@RestControllerAdvice
public class GlobalExceptionHandler {
// 捕获异常后调用这个方法
@ExceptionHandler(Exception.class)
public Result handleException(Exception e){
e.printStackTrace();
return Result.error("出错了");
}
@ExceptionHandler(MyException.class)
public Result handleMyException(MyException e){
e.printStackTrace();
return Result.error(e.getMessage());
}
}