响应类
@Builder
public class Response<T> {
private Integer code;
private String message;
private Boolean success;
private Long t;//时间戳
private T data;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMessage() {
return message;
}
public Boolean getSuccess() {
return success;
}
public void setSuccess(Boolean success) {
this.success = success;
}
public void setMessage(String message) {
this.message = message;
}
public Long getT() {
return t;
}
public void setT(Long t) {
this.t = t;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
状态码
public enum ResponseCode {
//自定义一些请求
SUCCESS(200,"操作成功",true),
FAIL(501,"业务异常",false),// 统指一切不好分类的业务异常
USER_SESSION_LOSS(1000,"用户sesssion丢失",false),
PARAMS_IS_EMPTY(1001,"参数不能为空",false),
ILLEGAL_PARAM(1002,"参数不正确",false),
DATA_EMPTY(1003,"数据为空",false),
SYSTEM_ERROR(500,"系统异常",false),
;
private Integer code;
private String message;
private Boolean success;
ResponseCode(Integer code,String message,Boolean success){
this.code=code;
this.message=message;
this.success=success;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Boolean getSuccess() {
return success;
}
public void setSuccess(Boolean success) {
this.success = success;
}
}
响应工具
@Builder
public class ResponseUtil {
/**
* 返回成功状态,包含数据
* @param data
* @param <T>
* @return
*/
public static <T> Response<T> buildSuccessResponse(T data){
return buildResponse(data, ResponseCode.SUCCESS);
}
/**
* 返回成功状态,不包含数据
* @return
*/
public static Response success(){
return buildResponse(ResponseCode.SUCCESS);
}
/**
* 返回失败状态
* @param responseCode
* @return
*/
public static Response buildFailResponse(ResponseCode responseCode){
ResponseCode.FAIL.setMessage(responseCode.getMessage());
return buildResponse(ResponseCode.FAIL);
}
/**
* 返回Response,包含数据
* @param data
* @param responseCode
* @return
*/
public static Response buildResponse(Object data,ResponseCode responseCode){
return Response.builder()
.code(responseCode.getCode())
.message(responseCode.getMessage())
.success(responseCode.getSuccess())
.t(System.currentTimeMillis())
.data(data)
.build();
}
/**
* 返回success Response,包含数据
* @param data
* @return
*/
public static Response genSuccessResponse(Object data){
ResponseCode resp = ResponseCode.SUCCESS;
return Response.builder()
.code(resp.getCode())
.message(resp.getMessage())
.success(resp.getSuccess())
.t(System.currentTimeMillis())
.data(data)
.build();
}
/**
* 返回状态ResponseStatus
* @param responseCode
* @return
*/
public static Response buildResponse(ResponseCode responseCode){
return Response.builder()
.code(responseCode.getCode())
.message(responseCode.getMessage())
.success(responseCode.getSuccess())
.t(System.currentTimeMillis())
.build();
}
}```
在控制层返回日志
```java
return ResponseUtil.buildResponse(service.query(id), ResponseCode.SUCCESS);
觉得有用的小伙伴们点个赞呗