自定义编码
package mybatis.utils.result;
public enum ApiCodeEnum{
SUCESS(200, "请求成功"),
PARAMS_FORMAT_ERROR(300, "参数异常,请联系管理员"),
INTERFACE_ERROR(400, "接口异常,请联系管理员"),
FAIL(500, "系统异常,请联系管理员");
private final int code;
private final String msg;
ApiCodeEnum(int code, String msg) {
this.code = code;
this.msg = msg;
}
public int getCode() {
return code;
}
public String getMsg() {
return msg;
}
}
自定义异常
package mybatis.utils.result;
public class ApiException extends RuntimeException {
private int exCode;
private String exMsg;
public ApiException(int exCode, String exMsg) {
this.exCode = exCode;
this.exMsg = exMsg;
}
public static void fail() {
throw new ApiException(ApiCodeEnum.FAIL.getCode(), ApiCodeEnum.FAIL.getMsg());
}
public static void fail(String msg) {
throw new ApiException(ApiCodeEnum.FAIL.getCode(), msg);
}
public static void interfaceFail() {
throw new ApiException(ApiCodeEnum.INTERFACE_ERROR.getCode(), ApiCodeEnum.INTERFACE_ERROR.getMsg());
}
public static void interfaceFail(String msg) {
throw new ApiException(ApiCodeEnum.INTERFACE_ERROR.getCode(), msg);
}
public static void paramsFail() {
throw new ApiException(ApiCodeEnum.PARAMS_FORMAT_ERROR.getCode(), ApiCodeEnum.PARAMS_FORMAT_ERROR.getMsg());
}
public static void paramsFail(String msg) {
throw new ApiException(ApiCodeEnum.PARAMS_FORMAT_ERROR.getCode(), msg);
}
public int getExCode() {
return exCode;
}
public void setExCode(int exCode) {
this.exCode = exCode;
}
public String getExMsg() {
return exMsg;
}
public void setExMsg(String exMsg) {
this.exMsg = exMsg;
}
}
自定义返回结果格式
package mybatis.utils.result;
import java.io.Serializable;
public class ApiResult<T> implements Serializable {
private static final long serialVersionUID = 411731814484355577L;
private int code;
private String msg;
private T data;
public ApiResult(int code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public static <T> ApiResult<T> success(T t) {
return new ApiResult<>(ApiCodeEnum.SUCESS.getCode(), ApiCodeEnum.SUCESS.getMsg(), t);
}
public static <T> ApiResult<T> success(T t, String msg) {
return new ApiResult<>(ApiCodeEnum.SUCESS.getCode(), msg, t);
}
public static <T> ApiResult<T> fail() {
return new ApiResult<>(ApiCodeEnum.FAIL.getCode(), ApiCodeEnum.FAIL.getMsg(), null);
}
public static <T> ApiResult<T> fail(String msg) {
return new ApiResult<>(ApiCodeEnum.FAIL.getCode(), msg, null);
}
public static <T> ApiResult<T> fail(T t, String msg) {
return new ApiResult<>(ApiCodeEnum.FAIL.getCode(), msg, t);
}
public static <T> ApiResult<T> paramsFail() {
return new ApiResult<>(ApiCodeEnum.PARAMS_FORMAT_ERROR.getCode(), ApiCodeEnum.PARAMS_FORMAT_ERROR.getMsg(), null);
}
public static <T> ApiResult<T> paramsFail(String msg) {
return new ApiResult<>(ApiCodeEnum.PARAMS_FORMAT_ERROR.getCode(), msg, null);
}
public static <T> ApiResult<T> paramsFail(T t, String msg) {
return new ApiResult<>(ApiCodeEnum.PARAMS_FORMAT_ERROR.getCode(), msg, t);
}
public static <T> ApiResult<T> interfaceFail() {
return new ApiResult<>(ApiCodeEnum.INTERFACE_ERROR.getCode(), ApiCodeEnum.INTERFACE_ERROR.getMsg(), null);
}
public static <T> ApiResult<T> interfaceFail(String msg) {
return new ApiResult<>(ApiCodeEnum.INTERFACE_ERROR.getCode(), msg, null);
}
public static <T> ApiResult<T> interfaceFail(T t, String msg) {
return new ApiResult<>(ApiCodeEnum.INTERFACE_ERROR.getCode(), msg, t);
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
异常拦截
package mybatis.utils.result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(ApiException.class)
public ApiResult handlerApiException(ApiException e) {
return new ApiResult(e.getExCode(), e.getExMsg(), null);
}
@ExceptionHandler(Exception.class)
public ApiResult handlerException(Exception e) {
return ApiResult.fail(e.getMessage());
}
}
对jdk的断言方法进行了增强
package mybatis.utils.asserts;
import mybatis.utils.result.ApiCodeEnum;
import mybatis.utils.result.ApiException;
import java.util.List;
import java.util.Map;
public class AssertUtil {
public static void objIsNull(Object o) {
check(o, ApiCodeEnum.PARAMS_FORMAT_ERROR.getMsg());
}
public static void objIsNull(Object o, String msg) {
check(o, msg);
}
private static void check(Object o, String msg) {
if (null == o) {
throwException(msg);
}
if (o instanceof String) {
if ("".equals(o))
throwException(msg);
}
if (o instanceof List) {
if (((List) o).size() == 0)
throwException(msg);
}
if (o instanceof Map) {
if (((Map) o).size() == 0)
throwException(msg);
}
if (o instanceof Boolean) {
if ((Boolean) o == false) {
throwException(msg);
}
}
}
private static void throwException(String msg) {
ApiException.paramsFail(msg);
}
}
测试类
@RestController
public class TestContrller {
@Autowired
private TestService service;
@GetMapping("/test")
public ApiResult entryTest(@RequestParam Map<String, Object> map) {
return ApiResult.success(service.entryService(map));
}
}
@Component
public class TestService {
public Map<String, Object> entryService(Map<String, Object> map) {
Object name = map.get("name");
int i = new Random().nextInt(4);
if (i == 1) {
AssertUtil.objIsNull(name);
}
if (i == 2) {
System.out.println(1 / 0);
}
if (1 == 3) {
ApiException.interfaceFail();
}
if (i==4){
ApiException.paramsFail();
}
return (map);
}
}