http响应-统一响应格式

This commit is contained in:
2025-09-18 11:25:57 +08:00
parent da04f6b766
commit d36c15438f
2 changed files with 20 additions and 3 deletions

View File

@@ -49,7 +49,7 @@ public class ResponseController {
@RequestMapping("/hello") @RequestMapping("/hello")
public Result hello(){ public Result hello(){
System.out.println("hello World"); System.out.println("hello World");
return new Result(1,"成功","hello world"); return Result.success("hello world");
} }
//响应对象 //响应对象
@@ -58,7 +58,7 @@ public class ResponseController {
Address address = new Address(); Address address = new Address();
address.setProvince("江苏"); address.setProvince("江苏");
address.setCity("常州"); address.setCity("常州");
return new Result(1,"成功",address); return Result.success(address);
} }
//响应集合 //响应集合
@@ -76,7 +76,7 @@ public class ResponseController {
address1.setCity("苏州"); address1.setCity("苏州");
list.add(address1); list.add(address1);
return new Result(1,"成功",list); return Result.success(list);
} }
} }

View File

@@ -46,4 +46,21 @@ public class Result {
", data=" + data + ", data=" + data +
'}'; '}';
} }
//封装一些固定的成功和失败的方法,供外部调用
public static Result success(){
/*Result result = new Result(1, "成功", null);
return result;*/
return success(null);
}
public static Result success(Object data){
Result result = new Result(1, "成功", data);
return result;
}
public static Result error(String msg){
Result result = new Result(0, msg, null);
return result;
}
} }