JFinal中Ret类大家应该都非常熟悉,几乎是必定会用到的一个工具类
用法如下:
Ret.ok().set("message","xxxx");
稍作封装:
RetKit.ok("xxxxx");
public class RetKit extends Ret {
private static final String MSG = "message";
private static final String CODE = "code";
public static Ret ok() {
return Ret.ok(CODE, 0);
}
public static Ret fail() {
return Ret.fail(CODE, -1);
}
public static Ret ok(String message) {
return ok().set(MSG, message);
}
public static Ret ok(String key, Object value) {
return ok().set(key, value);
}
public static Ret fail(String message) {
return fail().set(MSG, message);
}
}增加code是因为前端某些UI框架会从返回的字段中获取code值判断成功或失败,例如:layuiadmin
controller中方法:
public Ret doSend() {
//业务逻辑xxxx
return RetKit.fail("消息发送失败");
//return RetKit.ok("消息发送成功");
}不知道大家有没做这样小玩意的封装 ^-^ ?
public static Ret code(int code) {
return Ret.create(CODE, code);
}
有些较为复杂的系统,会有很多 code 来代替各种不同的状态,例如微信公众号就有几十个 code 值
为了节省流量,使用这些 code 值时,不需要设置 msg 值,而是让开发者去看文档中 code 值的对应含义