简介
- 采用泛型类,用于存一个泛型变量用于存储业务数据
- 序列化需实现
Serializable
- 主要就3个属性:消息,状态码,业务数据
- 状态码都设置为
static final
- 状态码都设置为
- 序列化需实现
- 主要就ok,failmsg,error那几个基本方法,他们会创建HttpResult对象,其他ok,fail,error泛型方法也是掉他们来获取对象。再其他些方法就是些set,get方法
- ok等方法默认code就是200 ```java package com.yxkj.common.core.util.httpUtils;
import java.io.Serializable;
/**
- 返回Json数据模板
@param
/ public class HttpResult implements Serializable { /* - 请求成功 / public static final int SUCCESS = 200; /*
无权限 */ public static final int N0_AUTHORITY = 403;
/**
未登录 */ public static final int NO_LOGIN = 401;
/**
服务器内部错误 */ public static final int DEFAULT_ERROR = 400;
private int code;
private String msg;
private T data;
private Object object;
public int getCode() { return code; }
public HttpResult
setCode(int code) { this.code = code; return this; } public String getMsg() { return msg; }
public HttpResult
setMsg(String msg) { this.msg = msg; return this; } public T getData() { return data; }
public HttpResult
setData(T data) { this.data = data; return this; } public static
HttpResult ok(T data){ HttpResult ok = ok(); ok.setData(data); return ok; } public static
HttpResult ok(T data,Object object){ HttpResult ok = ok(); ok.setData(data); ok.setObject(object); return ok; } public static
HttpResult ok(){ HttpResult httpResult = new HttpResult<>(); httpResult.setCode(SUCCESS); httpResult.setMsg(“success”); return httpResult; } public static HttpResult failMsg(String msg) { HttpResult restResult = new HttpResult(); restResult.setCode(DEFAULT_ERROR); restResult.setMsg(msg); return restResult; }
public static
HttpResult fail(){ HttpResult httpResult = new HttpResult<>(); httpResult.setCode(NO_LOGIN); httpResult.setMsg(“请重新登录!”); return httpResult; } public static
HttpResult error(T data){ HttpResult error = error(); if (data instanceof String){ error.setMsg((String) data);
} error.setData(data); return error; }
/**
- 自定义错误码错误
- @param data T
- @param code code 403
- @param
data @return HttpResult
*/ public static HttpResult error(T data, int code){ HttpResult error = error(); error.setCode(code); error.setData(data); return error; } /**
- 自定义错误码错误
- @param
data @return HttpResult
*/ public static HttpResult errorMsg(String msg){ HttpResult error = error(); error.setMsg(msg); return error; } public static
HttpResult errorMsgData(String msg,T data){ HttpResult error = error(); error.setCode(DEFAULT_ERROR); error.setMsg(msg); error.setData(data); return error; } public static
HttpResult okMsg(String msg,T data){ HttpResult httpResult = new HttpResult<>(); httpResult.setCode(SUCCESS); httpResult.setMsg(msg); httpResult.setData(data); return httpResult; } public static
HttpResult okMsgs(String msg){ HttpResult httpResult = new HttpResult<>(); httpResult.setCode(SUCCESS); httpResult.setMsg(msg); return httpResult; } /**
- 内部错误
- @param
t @return HttpResult
*/ public static HttpResult error(){ HttpResult httpResult = new HttpResult<>(); httpResult.setCode(DEFAULT_ERROR); httpResult.setMsg(“error”); return httpResult; } public static HttpResult
noAuthority(){ return new HttpResult
().setCode(N0_AUTHORITY).setMsg(“权限不足!”).setData(“权限不足!”); } public Object getObject() { return object; }
public void setObject(Object object) { this.object = object; }
@Override public String toString() { return “HttpResult{“ +
"code=" + code + ", msg='" + msg + '\'' + ", data=" + data + ", object=" + object + '}';
} }
<a name="ay2wc"></a>
## 其他开源案例
```java
package com.xkcoding.exception.handler.model;
import com.xkcoding.exception.handler.constant.Status;
import com.xkcoding.exception.handler.exception.BaseException;
import lombok.Data;
/**
* <p>
* 通用的 API 接口封装
* </p>
*
* @author yangkai.shen
* @date Created in 2018-10-02 20:57
*/
@Data
public class ApiResponse {
/**
* 状态码
*/
private Integer code;
/**
* 返回内容
*/
private String message;
/**
* 返回数据
*/
private Object data;
/**
* 无参构造函数
*/
private ApiResponse() {
}
/**
* 全参构造函数
*
* @param code 状态码
* @param message 返回内容
* @param data 返回数据
*/
private ApiResponse(Integer code, String message, Object data) {
this.code = code;
this.message = message;
this.data = data;
}
/**
* 构造一个自定义的API返回
*
* @param code 状态码
* @param message 返回内容
* @param data 返回数据
* @return ApiResponse
*/
public static ApiResponse of(Integer code, String message, Object data) {
return new ApiResponse(code, message, data);
}
/**
* 构造一个成功且带数据的API返回
*
* @param data 返回数据
* @return ApiResponse
*/
public static ApiResponse ofSuccess(Object data) {
return ofStatus(Status.OK, data);
}
/**
* 构造一个成功且自定义消息的API返回
*
* @param message 返回内容
* @return ApiResponse
*/
public static ApiResponse ofMessage(String message) {
return of(Status.OK.getCode(), message, null);
}
/**
* 构造一个有状态的API返回
*
* @param status 状态 {@link Status}
* @return ApiResponse
*/
public static ApiResponse ofStatus(Status status) {
return ofStatus(status, null);
}
/**
* 构造一个有状态且带数据的API返回
*
* @param status 状态 {@link Status}
* @param data 返回数据
* @return ApiResponse
*/
public static ApiResponse ofStatus(Status status, Object data) {
return of(status.getCode(), status.getMessage(), data);
}
/**
* 构造一个异常且带数据的API返回
*
* @param t 异常
* @param data 返回数据
* @param <T> {@link BaseException} 的子类
* @return ApiResponse
*/
public static <T extends BaseException> ApiResponse ofException(T t, Object data) {
return of(t.getCode(), t.getMessage(), data);
}
/**
* 构造一个异常且带数据的API返回
*
* @param t 异常
* @param <T> {@link BaseException} 的子类
* @return ApiResponse
*/
public static <T extends BaseException> ApiResponse ofException(T t) {
return ofException(t, null);
}
}