Response
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
/**
* 业务响应VO
*
* @author SC
* @since 1.0.0
*/
public class Response<T> extends ResponseEntity<ResponseBody<T>> {
public Response() {
super(new ResponseBody<>(ResponseCode.SUCCESS, null), HttpStatus.OK);
}
public Response(HttpStatus status) {
super(status);
}
public Response(ResponseBody<T> body, HttpStatus status) {
super(body, status);
}
public Response(MultiValueMap<String, String> headers, HttpStatus status) {
super(headers, status);
}
public Response(ResponseBody<T> body, MultiValueMap<String, String> headers, HttpStatus status) {
super(body, headers, status);
}
public void setCode(int code) {
ResponseBody<T> body = getResponseBody();
body.setCode(code);
}
public void setMsg(String msg) {
ResponseBody<T> body = getResponseBody();
body.setMsg(msg);
}
public void setData(T data) {
ResponseBody<T> body = getResponseBody();
body.setData(data);
}
public int getCode() {
ResponseBody<T> body = getResponseBody();
return body.getCode();
}
public String getMsg() {
ResponseBody<T> body = getResponseBody();
return body.getMsg();
}
public T getData() {
ResponseBody<T> body = getResponseBody();
return body.getData();
}
private ResponseBody<T> getResponseBody() {
ResponseBody<T> body = this.getBody();
if (body == null) {
throw new IllegalStateException("ResponseBody is null!");
}
return body;
}
}
ResponseBody
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
/**
* 业务返回结果类
*
* @author SC
* @since 1.0.0
*/
@Getter
@Setter
@ToString
@ApiModel("业务返回结果")
public class ResponseBody<T> {
@ApiModelProperty("错误码")
private int code;
@ApiModelProperty("错误信息")
private String msg;
@ApiModelProperty("返回结果")
private T data;
public ResponseBody(int code) {
this.code = code;
}
public ResponseBody(int code, String msg) {
this.code = code;
this.msg = msg;
}
public ResponseBody(int code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
}
ResponseCode
/**
* Description: 返回码常量定义
*
* @author SC
* @since 1.0.0
*/
public final class ResponseCode {
/**
* 成功
*/
public static final int SUCCESS = 200;
/**
* 失败
*/
public static final int FAILURE = 300;
/**
* CA未认证
*/
public static final int CA_VERFIY = 304;
/**
* 未登录
*/
public static final int NOT_LOGIN = 401;
/**
* session 过期
*/
public static final int SESSION_EVICT = 402;
/**
* 无权限
*/
public static final int PERMISSION_DENIED = 403;
/**
* 资源未找到
*/
public static final int NOT_FOUND = 404;
/**
* 无法获取用户信息
*/
public static final int USER_INFO_NOT_FOUND = 405;
/**
* 系统错误
*/
public static final int SYSTEM_ERROR = 500;
/**
* 参数校验错误
*/
public static final int PARAMETERS_VALIDATION_ERROR = 501;
/**
* 业务错误
*/
public static final int BUSINESS_ERROR = 502;
/**
* 服务不可达
*/
public static final int SERVICE_UNAVAILABLE = 503;
private ResponseCode() {
}
}
ResponseFactory
import org.springframework.http.HttpStatus;
/**
* Http响应工厂类
*
* @author SC
* @since 1.0.0
*/
public final class ResponseFactory {
private ResponseFactory() {
}
/**
* 构造Http响应,Http Status设为200
*
* @param code body中code字段
* @param msg body中msg字段
*/
public static <T> Response<T> build(int code, String msg) {
return new Response<>(new ResponseBody<>(code, msg), HttpStatus.OK);
}
/**
* 构造Http响应
*
* @param httpStatus Http响应状态码
* @param code body中code字段
* @param msg body中msg字段
*/
public static <T> Response<T> build(HttpStatus httpStatus, int code, String msg) {
return new Response<>(new ResponseBody<>(code, msg), httpStatus);
}
/**
* 构造Http响应,Http Status设为200
*
* @param code body中code字段
* @param msg body中msg字段
* @param data body中data字段
*/
public static <T> Response<T> build(int code, String msg, T data) {
return new Response<>(new ResponseBody<>(code, msg, data), HttpStatus.OK);
}
/**
* 构造Http响应
*
* @param httpStatus Http响应状态码
* @param code body中code字段
* @param msg body中msg字段
* @param data body中data字段
*/
public static <T> Response<T> build(HttpStatus httpStatus, int code, String msg, T data) {
return new Response<>(new ResponseBody<>(code, msg, data), httpStatus);
}
/**
* 构造成功响应,Http Status设为200
*/
public static <T> Response<T> success() {
return new Response<>(new ResponseBody<>(ResponseCode.SUCCESS, ResponseMsg.SUCCESS), HttpStatus.OK);
}
/**
* 构造成功响应,Http Status设为200
*
* @param data body中data字段
*/
public static <T> Response<T> success(T data) {
return new Response<>(new ResponseBody<>(ResponseCode.SUCCESS, ResponseMsg.SUCCESS, data), HttpStatus.OK);
}
/**
* 构造成功响应,Http Status设为200
*
* @param msg body中msg字段
* @param data body中data字段
*/
public static <T> Response<T> success(String msg, T data) {
return new Response<>(new ResponseBody<>(ResponseCode.SUCCESS, msg, data), HttpStatus.OK);
}
/**
* 构造失败响应,Http Status设为200
*/
public static <T> Response<T> failure() {
return new Response<>(new ResponseBody<>(ResponseCode.FAILURE, ResponseMsg.FAILURE), HttpStatus.OK);
}
/**
* 构造失败响应,Http Status设为200
*
* @param msg body中msg字段
*/
public static <T> Response<T> failure(String msg) {
return new Response<>(new ResponseBody<>(ResponseCode.FAILURE, msg), HttpStatus.OK);
}
/**
* 构造失败响应,Http Status设为200
*
* @param msg body中msg字段
* @param data body中data字段
*/
public static <T> Response<T> failure(String msg, T data) {
return new Response<>(new ResponseBody<>(ResponseCode.FAILURE, msg, data), HttpStatus.OK);
}
/**
* 构造失败响应,Http Status设为200
*
* @param code body中code字段
* @param msg body中msg字段
* @param data body中data字段
*/
public static <T> Response<T> failure(int code, String msg, T data) {
return new Response<>(new ResponseBody<>(code, msg, data), HttpStatus.OK);
}
/**
* 构造失败响应,Http Status设为200
*
* @param code body中code字段
* @param msg body中msg字段
* @param data body中data字段
*/
public static <T> Response<T> failure(HttpStatus status, int code, String msg, T data) {
return new Response<>(new ResponseBody<>(code, msg, data), status);
}
}
ResponseMsg
/**
* 返回描述常量定义
*
* @author SC
* @since 1.0.0
*/
public final class ResponseMsg {
/**
* 成功
*/
public static final String SUCCESS = "操作成功!";
/**
* 默认业务异常描述
*/
public static final String FAILURE = "操作失败!";
/**
* 默认错误描述
*/
public static final String DEFAULT_ERROR = "服务器开小差了,请稍后再试!";
/**
* 默认参数校验错误描述
*/
public static final String PARAMETERS_VALIDATION_ERROR = "参数不合法!";
public static final String JSON_NOT_READABLE_ERROR = "Json解析异常!";
public static final String MAX_UPLOAD_SIZE_EXCEEDED_ERROR = "文件大小超过%sMB限制!";
/**
* 默认业务异常描述
*/
public static final String BUSINESS_ERROR = "发生业务异常!";
/**
* 无权限访问提示
*/
public static final String PERMISSION_DENIED = "您无权限访问!";
/**
* 未登录
*/
public static final String NOT_LOGIN = "未登录!";
/**
* 资源未找到
*/
public static final String NOT_FOUND = "资源未找到!";
/**
* 获取用户信息异常
*/
public static final String GET_USER_INFO_EXCEPTION = "获取用户信息异常!";
/**
* 登录失败
*/
public static final String LOGIN_FAILURE = "登录失败!";
/**
* 登录成功
*/
public static final String LOGIN_SUCCESS = "登录成功!";
/**
* 注销成功
*/
public static final String LOGOUT_SUCCESS = "注销成功!";
/**
* Session 过期
*/
public static final String SESSION_EVICT = "您的会话已过期!";
private ResponseMsg() {
}
}