状态类
package com.neu.common.constant;
import lombok.Getter;
public enum CommonStatusEnum {
SUCCESS(200,"Success - 请求完成"),
FAIL(-1,"Fail - 请求失败")
;
@Getter
private int code;
@Getter
private String value;
CommonStatusEnum(int code,String value){
this.code = code;
this.value = value;
}
}
响应类
package com.neu.common.dto;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.neu.common.constant.CommonStatusEnum;
import lombok.Data;
import lombok.experimental.Accessors;
import java.util.Date;
@Data
@Accessors(chain = true)
public class ResponseResult<T> {
private int code;
private String message;
private T data;
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date time;
public static <T> ResponseResult success(){
return new ResponseResult()
.setCode(CommonStatusEnum.SUCCESS.getCode())
.setMessage(CommonStatusEnum.SUCCESS.getValue())
.setTime(new Date());
}
public static <T> ResponseResult success(T data){
return new ResponseResult()
.setCode(CommonStatusEnum.SUCCESS.getCode())
.setMessage(CommonStatusEnum.SUCCESS.getValue())
.setData(data)
.setTime(new Date());
}
public static <T> ResponseResult fail(int code,String message){
return new ResponseResult()
.setCode(code)
.setMessage(message)
.setTime(new Date());
}
public static <T> ResponseResult fail(int code,String message,String data){
return new ResponseResult()
.setCode(code)
.setMessage(message)
.setData(data)
.setTime(new Date());
}
public static <T> ResponseResult fail(T data){
return new ResponseResult()
.setData(data)
.setTime(new Date());
}
}
效果