标签:tostring style base str result err amp users 自定义类
一.通过Result类实现返回数据
//用户注册 @PostMapping("/registerUser") public Result registerUser(@RequestBody User user){ Result result = Results.successWithData( userService.RegisterUser(user) , BaseEnums.SUCCESS.code() , BaseEnums.SUCCESS.desc()) ; return result ; }
二.自定义类实现返回数据
package com.example.online_xdclass.Utils; import javax.swing.*; public class JsonData { //状态码,0表示成功,1表示处理中,-1表示失败 private Integer code; //业务数据 private Object data; //信息描述 private String msg; public JsonData(){ } public JsonData(Integer code,Object data,String msg){ this.code=code; this.data=data; this.msg=msg; } //成功,不返回数据 public static JsonData buildSucess(){ return new JsonData(0,null,null); } //成功,返回数据 public static JsonData buildSucess(Object data){ return new JsonData(0,data,null); } //失败,返回错误信息 public static JsonData buildError(String msg){ return new JsonData(-1,null,msg); } //失败,返回自定义状态码以及错误信息 public static JsonData buildError(Integer code,String msg){ return new JsonData(code,null,msg); } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } @Override public String toString() { return "JsonData{" + "code=" + code + ", data=" + data + ", msg=‘" + msg + ‘\‘‘ + ‘}‘; } }
spring boot项目可通过Result类或者自定返回类实现controller层返回数据
标签:tostring style base str result err amp users 自定义类
原文地址:https://www.cnblogs.com/datadogapache/p/14954103.html