統合パッケージjson戻り結果
8970 ワード
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import java.io.Serializable;
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
// json , null ,key
public class ServiceResponse implements Serializable {
private int status; //
private String msg; //
private T data; //
private ServiceResponse(int status){
this.status = status;
}
private ServiceResponse(int status, T data){
this.status = status;
this.data = data;
}
private ServiceResponse(int status, String msg, T data){
this.status = status;
this.msg = msg;
this.data = data;
}
private ServiceResponse(int status, String msg){
this.status = status;
this.msg = msg;
}
@JsonIgnore // json
public boolean isSuccess(){
return this.status == ResponseCode.SUCCESS.getCode();
}
public int getStatus() {
return status;
}
public String getMsg() {
return msg;
}
public T getData() {
return data;
}
public static ServiceResponse createBySuccess(){
return new ServiceResponse(ResponseCode.SUCCESS.getCode());
}
public static ServiceResponse createBySuccessMessage(String msg){
return new ServiceResponse(ResponseCode.SUCCESS.getCode(), msg);
}
public static ServiceResponse createBySuccess(T data){
return new ServiceResponse(ResponseCode.SUCCESS.getCode(), data);
}
public static ServiceResponse createBySuccess(String msg, T data){
return new ServiceResponse(ResponseCode.SUCCESS.getCode(), msg, data);
}
public static ServiceResponse createByError(){
return new ServiceResponse(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getDesc());
}
public static ServiceResponse createByErrorMessage(String errorMessage){
return new ServiceResponse(ResponseCode.ERROR.getCode(), errorMessage);
}
public static ServiceResponse createByErrorCodeMessage(int errorCode, String errorMessage){
return new ServiceResponse(errorCode, errorMessage);
}
}
使用した列挙値
/**
*
*/
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 desc;
ResponseCode(int code, String desc){
this.code = code;
this.desc = desc;
}
public int getCode() {
return code;
}
public String getDesc() {
return desc;
}
}