- 依赖:lombok
org.projectlombok lombok true
项目common结构:
CommonResponse写法
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonInclude; import lombok.Getter; import java.io.Serializable; @Getter @JsonInclude(JsonInclude.Include.NON_NULL) public class CommonResponseimplements Serializable { private int status; private String msg; private T data; private CommonResponse(int status){ this.status=status; } private CommonResponse(int status,String msg){ this.status=status; this.msg=msg; } private CommonResponse(int status,String msg,T data){ this.status=status; this.msg=msg; this.data=data; } private CommonResponse(int status,T data){ this.status=status; this.data=data; } @JsonIgnore public boolean isSuccess(){ return this.status == ResponseCode.SUCCESS.getCode(); } public static CommonResponse createForSuccess(){ return new CommonResponse (ResponseCode.SUCCESS.getCode()); } public static CommonResponse createForSuccess(T data){ return new CommonResponse (ResponseCode.SUCCESS.getCode(),data); } public static CommonResponse createForSuccessMessage(String msg){ return new CommonResponse (ResponseCode.SUCCESS.getCode(),msg); } public static CommonResponse createForSuccess(String msg,T data){ return new CommonResponse (ResponseCode.SUCCESS.getCode(),msg,data); } public static CommonResponse createForError(){ return new CommonResponse (ResponseCode.ERROR.getCode(),ResponseCode.ERROR.getDescription()); } public static CommonResponse createForError(String msg){ return new CommonResponse (ResponseCode.ERROR.getCode(),msg); } public static CommonResponse createForError(int code,String msg){ return new CommonResponse (code,msg); } }
ResponseCode(枚举)的写法
import lombok.Getter;
@Getter
public enum ResponseCode {
SUCCESS(0,"SUCCESS"),
ERROR(1,"ERROR"),
NEED_LOGIN(10,"NEED_LOGIN"),
ILLEGAL_ARGUMENT(2,"ILLEGAL_ARGUMENT");
private final int code;
private final String description;
ResponseCode(int code,String description){
this.code=code;
this.description = description;
}
}



