在别人的项目中碰到一个后端返回前端结果一个比较好的模板,记录下来。
import java.util.HashMap;
import java.util.Map;
/**
* 返回类
*
*/
public class ResultModel extends HashMap<String,Object> {
private static final long serialVersionUID = 1L;
public ResultModel(){
put("code",0);
put("msg","success");
}
public static ResultModel error(){
return error(500,"please contact administrator!");
}
public static ResultModel error(String msg){
return error(500,msg);
}
public static ResultModel error(int code,String msg){
ResultModel r = new ResultModel();
r.put("code",code);
r.put("msg",msg);
return r;
}
public static ResultModel ok(){
return new ResultModel();
}
public static ResultModel ok(String msg){
ResultModel r = new ResultModel();
r.put("msg",msg);
return r;
}
public static ResultModel ok(Map<String,Object> map){
ResultModel r = new ResultModel();
r.putAll(map);
return r;
}
@Override
public ResultModel put(String key, Object value){
super.put(key,value);
return this;
}
}