com.baomidou.mybatisplus.extension.api.R Maven / Gradle / Ivy
Show all versions of mybatis-plus-extension Show documentation
/*
* Copyright (c) 2011-2020, hubin ([email protected]).
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.baomidou.mybatisplus.extension.api;
import java.io.Serializable;
import java.util.Optional;
import com.baomidou.mybatisplus.extension.enums.ApiErrorCode;
import com.baomidou.mybatisplus.extension.exceptions.ApiException;
import lombok.Data;
import lombok.experimental.Accessors;
/**
*
* REST API 返回结果
*
*
* @author hubin
* @since 2018-06-05
*/
@Data
@Accessors(chain = true)
public class R implements Serializable {
/**
* 业务错误码
*/
private long code;
/**
* 结果集
*/
private T data;
/**
* 描述
*/
private String msg;
public R() {
// to do nothing
}
public R(IErrorCode errorCode) {
errorCode = Optional.ofNullable(errorCode).orElse(ApiErrorCode.FAILED);
this.code = errorCode.getCode();
this.msg = errorCode.getMsg();
}
public static R ok(T data) {
return restResult(data, ApiErrorCode.SUCCESS);
}
public static R failed(String msg) {
return restResult(null, ApiErrorCode.FAILED.getCode(), msg);
}
public static R failed(IErrorCode errorCode) {
return restResult(null, errorCode);
}
public static R restResult(T data, IErrorCode errorCode) {
return restResult(data, errorCode.getCode(), errorCode.getMsg());
}
private static R restResult(T data, long code, String msg) {
R apiResult = new R<>();
apiResult.setCode(code);
apiResult.setData(data);
apiResult.setMsg(msg);
return apiResult;
}
public boolean ok() {
return ApiErrorCode.SUCCESS.getCode() == code;
}
/**
* 服务间调用非业务正常,异常直接释放
*/
public T serviceData() {
if (!ok()) {
throw new ApiException(this.msg);
}
return data;
}
}