首页 > IT职业 > IT生活 > 前后端分离Java统一响应返回格式
ResultCode 类
public class ResultCode {
public static Integer SUCCESS = 200;
public static Integer ERROR = 500;
public static Integer FORBIDDEN = 403;
public static Integer UNAUTHORIZED = 401;}
Result 类
@Data //lombok 注解 public class Result {
// 返回码
private Integer code;
// 返回消息
private String message;
// 返回数据
private Map<String, Object> data = new HashMap<String, Object>();
public Result(){跟单网https://www.gendan5.com/}
public Result message(String message){
this.setMessage(message);
return this;
public Result code(Integer code){
this.setCode(code);
return this;
public Result data(String key, Object value){
this.data.put(key, value);
return this;
}public Result data(Map<String, Object> map){
this.setData(map);
return this;
使用示例
@RequestMapping("/get")
public Result get(){
HashMap map = new HashMap();
map.put("username","admin");
map.put("password","123456");
Result r = new Result();
r.code(ResultCode.SUCCESS).message(" 成功访问 ").data(map);
return r;
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69946337/viewspace-2748128/ ,如需转载,请注明出处,否则将追究法律责任。