package com.mindskip.xzs.base;
|
|
import java.util.HashMap;
|
import java.util.Map;
|
|
public class RestResponse<T> extends HashMap<String, Object> {
|
private int code;
|
private String message;
|
private T response;
|
|
/**
|
* Instantiates a new Rest response.
|
*
|
* @param code the code
|
* @param message the message
|
*/
|
public RestResponse(int code, String message) {
|
this.put("code", code);
|
this.put("message", message);
|
}
|
|
/**
|
* Instantiates a new Rest response.
|
*
|
* @param code the code
|
* @param message the message
|
* @param response the response
|
*/
|
public static RestResponse response(int code, String message, Object response) {
|
RestResponse restResponse = new RestResponse();
|
restResponse.put("code", code);
|
restResponse.put("message", message);
|
restResponse.put("response", response);
|
return restResponse;
|
}
|
|
/**
|
* Fail rest response.
|
*
|
* @param code the code
|
* @param msg the msg
|
* @return the rest response
|
*/
|
public static RestResponse fail(Integer code, String msg) {
|
return new RestResponse<>(code, msg);
|
}
|
|
/**
|
* Ok rest response.
|
*
|
* @return the rest response
|
*/
|
public static RestResponse ok() {
|
SystemCode systemCode = SystemCode.OK;
|
return new RestResponse<>(systemCode.getCode(), systemCode.getMessage());
|
}
|
|
/**
|
* Ok rest response.
|
*
|
* @param <F> the type parameter
|
* @param response the response
|
* @return the rest response
|
*/
|
public static <F> RestResponse<F> ok(F response) {
|
SystemCode systemCode = SystemCode.OK;
|
return RestResponse.response(systemCode.getCode(), systemCode.getMessage(), response);
|
}
|
|
/**
|
* Gets code.
|
*
|
* @return the code
|
*/
|
public int getCode() {
|
return (int) this.get("code");
|
}
|
|
/**
|
* Gets message.
|
*
|
* @return the message
|
*/
|
public String getMessage() {
|
return (String) this.get("message");
|
}
|
|
|
/**
|
* Gets response.
|
*
|
* @return the response
|
*/
|
public T getResponse() {
|
return (T) this.get("response");
|
}
|
|
public RestResponse() {
|
}
|
|
@Override
|
public RestResponse put(String key, Object value) {
|
super.put(key, value);
|
return this;
|
}
|
}
|