人狠话不多,直接上代码,希望你喜欢.
||
||
实体类:Result
package xxxx.xxxx.entity;
import xxx.enums.ResultCode;
import lombok.Data;
import java.io.Serializable;
/**
* API 统一返回状态码
* Created by cxz on 18/7/12
*/
@Data
public class Result implements Serializable {
private static final long serialVersionUID = -3948389268046368059L;
private Integer code;
private String msg;
private Object data;
public Result() {}
public Result(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
/**
* 返回成功状态码
* 无数据
* @return SUCCESS
*/
public static Result success() {
Result result = new Result();
result.setResultCode(ResultCode.SUCCESS);
return result;
}
/**
* 返回成功状态码
* @return SUCCESS
*/
public static Result success(Object data) {
Result result = new Result();
result.setResultCode(ResultCode.SUCCESS);
result.setData(data);
return result;
}
/**
* 返回成功状态码
* @return SUCCESS
*/
public static Result imgSuccess(Object data) {
Result result = new Result();
result.setResultCode(ResultCode.IMAGE_UPLOAD_SUCCESS);
result.setData(data);
return result;
}
/**
* 返回失败状态码
* @return FAILURE
*/
public static Result imgFailure(Object data) {
Result result = new Result();
result.setResultCode(ResultCode.IMAGE_UPLOAD_ERROR);
result.setData(data);
return result;
}
/**
* 返回成功状态码
* 不是图片类型
* @return FAILURE
*/
public static Result noImageType(Object data) {
Result result = new Result();
result.setResultCode(ResultCode.NO_IMAGE_UPLOAD_TYPE);
result.setData(data);
return result;
}
/**
* 添加成功状态码
* 无数据
* @return INSERT
*/
public static Result insertSuccess() {
Result result = new Result();
result.setResultCode(ResultCode.DATA_INSERT_SUCCESS);
return result;
}
/**
* 更新成功状态码
* 无数据
* @return UPDATE
*/
public static Result updateSuccess() {
Result result = new Result();
result.setResultCode(ResultCode.DATA_UPDATE_SUCCESS);
return result;
}
/**
* 删除成功状态码
* 无数据
* @return DELETE
*/
public static Result deleteSuccess() {
Result result = new Result();
result.setResultCode(ResultCode.DATA_DELETE_SUCCESS);
return result;
}
/**
* 返回失败状态码
* 无数据
* @return FAILURE
*/
public static Result failure(ResultCode resultCode) {
Result result = new Result();
result.setResultCode(resultCode);
return result;
}
/**
* 返回失败状态码
* @return FAILURE
*/
public static Result failure(ResultCode resultCode, Object data) {
Result result = new Result();
result.setResultCode(resultCode);
result.setData(data);
return result;
}
/**
* 返回失败状态码
* @return FAIL
*/
public static Result fail(ResultCode resultCode, Object data) {
Result result = new Result();
result.setResultCode(resultCode);
result.setData(data);
return result;
}
/**
* 添加失败状态码
* 无数据
* @return INSERT
*/
public static Result insertFailure() {
Result result = new Result();
result.setResultCode(ResultCode.DATA_INSERT_FAILURE);
return result;
}
/**
* 更新失败状态码
* 无数据
* @return UPDATE
*/
public static Result updateFailure() {
Result result = new Result();
result.setResultCode(ResultCode.DATA_UPDATE_FAILURE);
return result;
}
/**
* 删除失败状态码
* 无数据
* @return DELETE
*/
public static Result deleteFailure() {
Result result = new Result();
result.setResultCode(ResultCode.DATA_DELETE_FAILURE);
return result;
}
/**
* 返回数据
* @return data
*/
private void setData(Object data) {
this.data = data;
}
/**
* 返回值
* @return code
* @return message
*/
public void setResultCode(ResultCode code) {
this.code = code.code();
this.msg = code.message();
}
}
接下来看枚举类型的工具类: ResultCode 类 不多少直接上代码
该类中的数字大家统一修改啊 我的是根据自己的实际业务而自定义
package xxx.enums;
import java.util.ArrayList;
import java.util.List;
/**
* API 统一返回状态码
* Created by cxz on 18/7/12
*/
public enum ResultCode {
/* 成功状态码 */
SUCCESS(1, "返回数据成功"),
/* 增删改查 100001-100009*/
DATA_INSERT_SUCCESS(1,"添加数据成功"),
DATA_UPDATE_SUCCESS(1,"更新数据成功"),
DATA_DELETE_SUCCESS(1,"删除数据成功"),
DATA_BE_INTERRELATED(100013,"数据关联,无法删除"),
........省略N行
/* 参数错误:200001-299999 */
PARAM_IS_INVALID(200001, "参数无效"),
PARAM_IS_BLANK(200002, "参数为空"),
........省略N行
/* 用户错误:300001-399999*/
USER_NOT_LOGGED_IN(300001, "用户未登录"),
USER_LOGIN_ERROR(300002, "账号不存在或密码错误"),
USER_ACCOUNT_FORBIDDEN(300003, "账号已被禁用"),
USER_NOT_EXIST(300004, "用户不存在"),
USER_HAS_EXISTED(300005, "用户已存在"),
........省略N行
/* 业务错误:400001-499999 */
SPECIFIED_QUESTIONED_USER_NOT_EXIST(400001, "某业务出现问题"),
/* 系统错误:500001-599999 */
SYSTEM_INNER_ERROR(500001, "系统繁忙,请稍后重试"),
/* 数据错误:600001-6999999 */
RESTFUL_DATA_NONE(600001, "数据未找到"),
DATA_IS_WRONG(600002, "数据有误"),
DATA_ALREADY_EXISTED(600003, "数据已存在"),
/* 接口错误:700001-799999 */
INTERFACE_INNER_INVOKE_ERROR(700001, "内部系统接口调用异常"),
INTERFACE_OUTER_INVOKE_ERROR(700002, "外部系统接口调用异常"),
INTERFACE_FORBID_VISIT(700003, "该接口禁止访问"),
INTERFACE_ADDRESS_INVALID(700004, "接口地址无效"),
INTERFACE_REQUEST_TIMEOUT(700005, "接口请求超时"),
INTERFACE_EXCEED_LOAD(700006, "接口负载过高"),
INTERFACE_HEADER_DATA(700007, "接口调用失败"),
........省略N行
/* 权限错误:800001-899999 */
PERMISSION_NO_ACCESS(800001, "无访问权限"),
/* 图片上传:1001-1999 */
IMAGE_UPLOAD_ERROR(1001, "图片上传失败"),
NO_IMAGE_UPLOAD_TYPE(1002, "上传的文件不是图片类型,请重新上传"),
IMAGE_UPLOAD_FAILURE(1003, "上传失败,请选择要上传的图片"),
/* 图片上传:2001-2999 */
IMAGE_UPLOAD_SUCCESS(2001, "图片上传成功");
private Integer code;
private String message;
ResultCode(Integer code, String message) {
this.code = code;
this.message = message;
}
public Integer code() {
return this.code;
}
public String message() {
return this.message;
}
/**
* 返回信息
* @return NAME
*/
public static String getMessage(String name) {
for (ResultCode item : ResultCode.values()) {
if (item.name().equals(name)) {
return item.message;
}
}
return name;
}
/**
* 返回信息
* @return CODE
*/
public static Integer getCode(String name) {
for (ResultCode item : ResultCode.values()) {
if (item.name().equals(name)) {
return item.code;
}
}
return null;
}
@Override
public String toString() {
return this.name();
}
/**
* 校验重复的code值
* @return
*/
public static void main(String[] args) {
ResultCode[] ApiResultCodes = ResultCode.values();
List<Integer> codeList = new ArrayList<Integer>();
for (ResultCode ApiResultCode : ApiResultCodes) {
if (codeList.contains(ApiResultCode.code)) {
System.out.println(ApiResultCode.code);
} else {
codeList.add(ApiResultCode.code());
}
}
}
}
接下来看最终的使用 都是干货 😄😄😄😄😄😄
if( 查询出来的数据 != null){
return Result.success(products);
return Result.success();
return Result.deleteSuccess()
....这里支持好几种返回 都在实体类中封装了 可以自行查看和使用
}else{
return Result.failure(ResultCode.RESTFUL_DATA_NONE);
return Result.failure(ResultCode.RESTFUL_DATA_NONE,data);
return Result.insertFailure()
... 失败的也有好几种方法我这里就不举栗了
}
好吧 再来张图