目的潮孽,返回json的數(shù)據(jù)格式谒麦,統(tǒng)一返回的數(shù)據(jù)結(jié)果集瀑梗。
自定義封裝的統(tǒng)一返回代碼:
public class Result {
/**
? ? * 是否成功
? ? */
? ? private boolean success;
? ? /**
? ? * 狀態(tài)碼
? ? */
? ? private Integercode;
? ? /**
? ? * 返回消息
? ? */
? ? private Stringmessage;
? ? /**
? ? * 返回數(shù)據(jù)
? ? */
? ? 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());
}