com.baomidou.mybatisplus.extension.api.ApiResult Maven / Gradle / Ivy
/*
* 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.util.Optional;
import com.baomidou.mybatisplus.extension.enums.ApiErrorCode;
import com.baomidou.mybatisplus.extension.exceptions.ApiException;
import lombok.Data;
/**
*
* REST API 返回结果
*
*
* @author hubin
* @since 2018-06-05
*/
@Data
public class ApiResult {
/**
* 业务错误码
*/
private String code;
/**
* 结果集
*/
private T data;
/**
* 描述
*/
private String msg;
public ApiResult() {
// to do nothing
}
public ApiResult(IErrorCode errorCode) {
errorCode = Optional.ofNullable(errorCode).orElse(ApiErrorCode.FAILED);
this.code = errorCode.getCode();
this.msg = errorCode.getMsg();
}
public static ApiResult ok(T data) {
return restResult(data, ApiErrorCode.SUCCESS);
}
public static ApiResult failed(String msg) {
return restResult(null, ApiErrorCode.FAILED.getCode(), msg);
}
public static ApiResult failed(IErrorCode errorCode) {
return restResult(null, errorCode);
}
public static ApiResult restResult(T data, IErrorCode errorCode) {
return restResult(data, errorCode.getCode(), errorCode.getMsg());
}
private static ApiResult restResult(T data, String code, String msg) {
ApiResult apiResult = new ApiResult<>();
apiResult.setCode(code);
apiResult.setData(data);
apiResult.setMsg(msg);
return apiResult;
}
public boolean ok() {
return ApiErrorCode.SUCCESS.getCode().equals(this.code);
}
/**
* 服务间调用非业务正常,异常直接释放
*/
public T serviceData() {
if (!ok()) {
throw new ApiException(this.msg);
}
return data;
}
}