All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.nxyfan.framework.common.pojo.CommonResult Maven / Gradle / Ivy
package com.nxyfan.framework.common.pojo;
import io.swagger.annotations.ApiModelProperty;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.service.ResponseMessage;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.nxyfan.framework.common.enums.CommonExceptionEnum;
/**
* 对Ajax请求返回Json格式数据的简易封装
*
* @author amour
* @date 2022/8/15 16:08
**/
public class CommonResult implements Serializable {
private static final long serialVersionUID = 1L;
public static final int CODE_SUCCESS = 100;
public static final int CODE_ERROR = 500;
@ApiModelProperty(value = "状态码")
private int rspCode;
@ApiModelProperty(value = "提示语")
private String rspMsg;
@ApiModelProperty(value = "返回数据")
private T data;
@JsonIgnore
private Exception exception;
public CommonResult() {}
public CommonResult(int rspCode, String rspMsg, T data) {
this.setRspCode(rspCode);
this.setRspMsg(rspMsg);
this.setData(data);
}
public CommonResult(int rspCode, String rspMsg, T data, Exception exception) {
this.setRspCode(rspCode);
this.setRspMsg(rspMsg);
this.setData(data);
this.setException(exception);
}
/**
* 获取code
* @return code
*/
public Integer getRspCode() {
return this.rspCode;
}
/**
* 获取msg
* @return msg
*/
public String getRspMsg() {
return this.rspMsg;
}
/**
* 获取data
* @return data
*/
public T getData() {
return this.data;
}
/**
* 给code赋值,连缀风格
* @param code code
* @return 对象自身
*/
public CommonResult setRspCode(int rspCode) {
this.rspCode = rspCode;
return this;
}
/**
* 给msg赋值,连缀风格
* @param msg msg
* @return 对象自身
*/
public CommonResult setRspMsg(String rspMsg) {
this.rspMsg = rspMsg;
return this;
}
/**
* 给data赋值,连缀风格
* @param data data
* @return 对象自身
*/
public CommonResult setData(T data) {
this.data = data;
return this;
}
/**
*
* Describe: 给exception赋值,连缀风格
* Author: Administrator
* Create Time: 2024年5月20日 下午2:46:19
* @param exception
* @return 对象自身
*/
public CommonResult setException(Exception exception) {
this.exception = exception;
return this;
}
// ============================ 构建 ==================================
// 构建成功
public static CommonResult ok() {
return new CommonResult<>(CODE_SUCCESS, "操作成功", null);
}
public static CommonResult ok(String rspMsg) {
return new CommonResult<>(CODE_SUCCESS, rspMsg, null);
}
public static CommonResult code(int rspCode) {
return new CommonResult<>(rspCode, null, null);
}
public static CommonResult data(T data) {
return new CommonResult<>(CODE_SUCCESS, "操作成功", data);
}
// 构建失败
public static CommonResult error() {
return new CommonResult<>(CODE_ERROR, "出错了,请联系工程师", null);
}
public static CommonResult error(String rspMsg) {
return new CommonResult<>(CODE_ERROR, rspMsg, null);
}
public static CommonResult error(String rspMsg, T data) {
return new CommonResult<>(CODE_ERROR, rspMsg, data);
}
public static CommonResult error(String rspMsg, Exception exception) {
return new CommonResult<>(CODE_ERROR, rspMsg, null, exception);
}
// 构建指定状态码
public static CommonResult get(int rspCode, String rspMsg, T data) {
return new CommonResult<>(rspCode, rspMsg, data);
}
public static CommonResult get(int rspCode, String rspMsg, T data, Exception exception) {
return new CommonResult<>(rspCode, rspMsg, data, exception);
}
/*
* toString()
*/
@Override
public String toString() {
return "{"
+ "\"rspCode\": " + this.getRspCode()
+ ", \"rspMsg\": \"" + this.getRspMsg() + "\""
+ ", \"data\": \"" + this.getData() + "\""
+ "}";
}
/**
* 响应状态码集合
*
* @author amour
* @date 2022/7/25 13:36
**/
public static List responseList() {
return Arrays.stream(CommonExceptionEnum.values()).map(commonExceptionEnum -> new ResponseMessageBuilder()
.code(commonExceptionEnum.getCode()).message(commonExceptionEnum.getMessage()).build())
.collect(Collectors.toList());
}
}