package com.nq.common;
|
|
import com.nq.utils.HttpRequest;
|
import com.nq.utils.translate.GoogleTranslateUtil;
|
import org.codehaus.jackson.annotate.JsonIgnore;
|
import org.codehaus.jackson.map.annotate.JsonSerialize;
|
import org.springframework.context.i18n.LocaleContextHolder;
|
|
import javax.servlet.http.HttpServletRequest;
|
import java.io.Serializable;
|
|
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
|
public class ServerResponse<T> extends Object implements Serializable {
|
private int status;
|
private String msg;
|
private T data;
|
|
private ServerResponse(int status) {
|
this.status = status;
|
}
|
|
private ServerResponse(int status, T data) {
|
this.status = status;
|
this.data = data;
|
}
|
|
private ServerResponse(int status, String msg) {
|
this.status = status;
|
this.msg = msg;
|
}
|
|
private ServerResponse(int status, String msg, T data) {
|
this.status = status;
|
this.msg = msg;
|
this.data = data;
|
}
|
|
public static void main(String[] args) {
|
ServerResponse serverResponse = new ServerResponse(1, new Object());
|
ServerResponse serverResponse1 = new ServerResponse(1, "abc");
|
System.out.print("ServerResponse");
|
}
|
|
@JsonIgnore
|
public boolean isSuccess() {
|
return (this.status == ResponseCode.SUCCESS.getCode());
|
}
|
|
public int getStatus() {
|
return this.status;
|
}
|
|
public T getData() {
|
return (T) this.data;
|
}
|
|
public void setData(T data) {
|
this.data = data;
|
}
|
|
public String getMsg() {
|
return this.msg;
|
}
|
|
public static <T> ServerResponse<T> createBySuccess() {
|
return new ServerResponse(ResponseCode.SUCCESS.getCode());
|
}
|
|
public static <T> ServerResponse<T> createBySuccessMsg(String msg, String lange) {
|
return new ServerResponse(ResponseCode.SUCCESS.getCode(), new GoogleTranslateUtil().translate(msg,lange));
|
}
|
|
public static <T> ServerResponse<T> createBySuccess(T data) {
|
return new ServerResponse(ResponseCode.SUCCESS.getCode(), data);
|
}
|
|
public static <T> ServerResponse<T> createBySuccess(String msg, T data,String lange) {
|
return new ServerResponse(ResponseCode.SUCCESS.getCode(), new GoogleTranslateUtil().translate(msg,lange), data);
|
}
|
|
|
public static <T> ServerResponse<T> createByError(String msg, T data,String lange) {
|
return new ServerResponse(ResponseCode.ERROR.getCode(), new GoogleTranslateUtil().translate(ResponseCode.ERROR.getMsg(), lange), data);
|
}
|
|
public static <T> ServerResponse<T> createByError(String lange) {
|
return new ServerResponse(ResponseCode.ERROR.getCode(),new GoogleTranslateUtil().translate(ResponseCode.ERROR.getMsg(), lange));
|
}
|
|
public static <T> ServerResponse<T> createByErrorMsg(String errormsg,String lange) {
|
return new ServerResponse(ResponseCode.ERROR.getCode(),new GoogleTranslateUtil().translate(errormsg, lange));
|
}
|
|
public static <T> ServerResponse<T> createByErrorCodeMsg(int errorcode, String errormsg,String lange) {
|
return new ServerResponse(errorcode, new GoogleTranslateUtil().translate(errormsg, lange));
|
}
|
}
|