1
  • 定义全局返回result
public class Result {

    /**
     * 状态响应码
     */
    private int code;

    /**
     * 响应结果 成功/失败
     */
    private boolean success;

    /**
     * 响应信息
     */
    private String message;

    /**
     * 响应数据
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Object data;

    /**
     * 数据总数
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Long totalCount;

    public Result setCode(ResultCode resultCode) {
        this.code = resultCode.code();
        return this;
    }

    public int getCode() {
        return code;
    }

    public boolean isSuccess() {
        return success;
    }

    public Result setSuccess(boolean success) {
        this.success = success;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public Result setMessage(String message) {
        this.message = message;
        return this;
    }

    public Object getData() {
        return data;
    }

    public Result setData(Object data) {
        this.data = data;
        return this;
    }

    public Long getTotalCount() { return totalCount;}

    public Result setTotalCount(Long totalCount) {
        this.totalCount = totalCount;
        return this;
    }

}
  • 定义全局resultCode
public enum ResultCode {

    SUCCESS(200),//成功
    FAIL(400),//失败
    UNAUTHORIZED(401),//未认证(签名错误) 未登录
    NOT_FOUND(404),//接口不存在
    INTERNAL_SERVER_ERROR(500);//服务器内部错误


    private final int code;

    ResultCode(int code) {
        this.code = code;
    }

    public int code() {
        return code;
    }

}
  • 定义返回工具类
@Slf4j
public class ReturnUtils {

    private static final String DEFAULT_SUCCESS_MESSAGE = "SUCCESS";

    public static Result success() {
        return new Result().setCode(ResultCode.SUCCESS)
                .setSuccess(true)
                .setMessage(DEFAULT_SUCCESS_MESSAGE);
    }

    public static Result success(String msg) {
        return new Result().setCode(ResultCode.SUCCESS)
                .setSuccess(true)
                .setMessage(msg);
    }

    public static Result success(String msg, Object data, long totalCount) {
        return new Result().setCode(ResultCode.SUCCESS)
                .setMessage(msg)
                .setData(data)
                .setSuccess(true)
                .setTotalCount(totalCount);
    }

    public static Result fail(String msg) {
        log.error(msg);
        return new Result().setCode(ResultCode.FAIL)
                .setSuccess(false)
                .setMessage(msg);
    }

    public static Result fail(ResultCode code, String msg) {
        log.error(msg);
        return new Result().setCode(code)
                .setSuccess(false)
                .setMessage(msg);
    }

}
  • 定义全局异常处理类
@Slf4j
@RestControllerAdvice
public class ExceptionAdvice {
    @ExceptionHandler(Exception.class)
    public Result exceptionHandler(HttpServletResponse resp, Exception e) {
        log.error(e.getMessage());
        return ReturnUtils.fail(ResultCode.INTERNAL_SERVER_ERROR, e.getMessage());
    }
}
  • 测试
@RequestMapping("test-exception")
    public void testException() {
        try {
            int i = 1 / 0;
        } catch (Exception e) {
            throw new RuntimeException("服务器发生错误,请联系管理员");
        }
    }
  • 结果
{
    "code":500,
    "success":false,
    "message":"服务器发生错误,请联系管理员"
}

代码


lijingyulee
91 声望7 粉丝