import lombok.Data;
import org.apache.http.HttpStatus;
import java.io.Serializable;
@Data //添加lombok依賴就可以使用該注解剑勾,非常方便床估,省去一大堆get和set方法
public class Result<T> implements Serializable {
private static final long serialVersionUID = 1L;
private int code;
private String msg;
private T data;
public Result() {
}
public Result(int code,String msg,T data){
this.code = code;
this.msg = msg;
this.data = data;
}
/**
* 只有msg和data入?yún)? * @param msg
* @param data
* @param <T>
* @return
*/
public static<T> Result<T> success(String msg,T data){
return new Result<>(HttpStatus.SC_OK,msg,data);
}
/**
* @description 只有msg入?yún)? * @param msg
* @param <T>
* @return
*/
public static<T> Result<T> success(String msg){
return Result.success(msg,null);
}
/**
* @description 只有data入?yún)? * @param data
* @param <T>
* @return
*/
public static<T> Result<T> success(T data){
return Result.success("操作成功",data);
}
/**
* @description 用于調(diào)用成功,沒有返參結(jié)果(void)的方法
* @param <T>
* @return
*/
public static<T> Result<T> success(){
return Result.success("操作成功");
}
public static<T> Result<T> error(String msg,T data){
return new Result<>(HttpStatus.SC_INTERNAL_SERVER_ERROR,msg,data);
}
public static<T> Result<T> error(String msg){
return Result.error(msg,null);
}
public static<T> Result<T> error(T data){
return Result.error("操作失敗",data);
}
public static<T> Result<T> error(){
return Result.error("操作失敗");
}
}
2.指定泛型結(jié)構(gòu)
import com.example.springboot.common.core.constant.HttpStatus;
import com.example.springboot.util.StringUtils;
import java.util.HashMap;
public class AjaxResult extends HashMap<String,Object> {
public static final String CODE = "code";
public static final String MSG = "msg";
public static final String Data = "data";
public AjaxResult() {
}
public AjaxResult(int code,String msg) {
super.put(CODE,code);
super.put(MSG,msg);
}
public AjaxResult(int code,String msg,Object data) {
super.put(CODE,code);
super.put(MSG,msg);
if (StringUtils.isNotNull(data)){
super.put(Data,data);
}
}
/**
* 返回成功消息
* @description msg和data入?yún)? * @param msg 返回內(nèi)容
* @param data 數(shù)據(jù)對象
* @return 成功消息
*/
public static AjaxResult success(String msg,Object data){
return new AjaxResult(HttpStatus.SUCCESS,msg,data);
}
/**
* @description 只有msg入?yún)? * @param msg
* @return
*/
public static AjaxResult success(String msg){
return AjaxResult.success(msg,null);
}
/**
* @description 只有data入?yún)? * @param data
* @return
*/
public static AjaxResult success(Object data){
return AjaxResult.success("操作成功",data);
}
/**
* @description 用于調(diào)用成功痰哨,沒有返參結(jié)果(void)的方法
* @return
*/
public static AjaxResult success(){
return AjaxResult.success("操作成功");
}
/**
* 返回錯誤消息
*
* @param msg 返回內(nèi)容
* @param data 數(shù)據(jù)對象
* @return 警告消息
*/
public static AjaxResult error(String msg,Object data){
return new AjaxResult(HttpStatus.ERROR,msg,data);
}
public static AjaxResult error(){
return AjaxResult.error("操作失敗");
}
public static AjaxResult error(String msg){
return AjaxResult.error(msg,null);
}
public static AjaxResult error(Object data){
return AjaxResult.error("操作失敗",data);
}
}
自定義返回狀態(tài)碼
/**
* 返回狀態(tài)碼
*/
public class HttpStatus {
/**
* 操作成功
*/
public static final int SUCCESS = 200;
/**
* 操作失敗
*/
public static final int ERROR = 500;
}