目的,返回json的数据格式,统一返回的数据结果集。
自定义封装的统一返回代码:
public class Result {
/**
* 是否成功
*/
private boolean success;
/**
* 状态码
*/
private Integercode;
/**
* 返回消息
*/
private Stringmessage;
/**
* 返回数据
*/
private Mapdata =new HashMap();
public static Resultok() {
Result result =new Result();
result.setSuccess(true);
result.setCode(200);
return result;
}
public static Resulterror() {
Result result =new Result();
result.setSuccess(false);
result.setCode(500);
return result;
}
public Resultdata(Object data) {
this.data.put("data",data);
return this;
}
public Resultdata(String key,Object data) {
this.data.put(key, data);
return this;
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public IntegergetCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public StringgetMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public MapgetData() {
return data;
}
public void setData(Map data) {
this.data = data;
}
}
使用:
@GetMapping("/testUser")
public Result getUsers() {
return Result.ok().data(userService.getUser());
}
网友评论