From d36c15438f279180afff9d99af32e273ee9e0cf5 Mon Sep 17 00:00:00 2001 From: xuxin <840198532@qq.com> Date: Thu, 18 Sep 2025 11:25:57 +0800 Subject: [PATCH] =?UTF-8?q?http=E5=93=8D=E5=BA=94-=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E5=93=8D=E5=BA=94=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../inmind/controller/ResponseController.java | 6 +++--- .../src/main/java/com/inmind/pojo/Result.java | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/springboot-web-req-resp/src/main/java/com/inmind/controller/ResponseController.java b/springboot-web-req-resp/src/main/java/com/inmind/controller/ResponseController.java index 2a062ce..7197716 100644 --- a/springboot-web-req-resp/src/main/java/com/inmind/controller/ResponseController.java +++ b/springboot-web-req-resp/src/main/java/com/inmind/controller/ResponseController.java @@ -49,7 +49,7 @@ public class ResponseController { @RequestMapping("/hello") public Result hello(){ 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.setProvince("江苏"); address.setCity("常州"); - return new Result(1,"成功",address); + return Result.success(address); } //响应集合 @@ -76,7 +76,7 @@ public class ResponseController { address1.setCity("苏州"); list.add(address1); - return new Result(1,"成功",list); + return Result.success(list); } } diff --git a/springboot-web-req-resp/src/main/java/com/inmind/pojo/Result.java b/springboot-web-req-resp/src/main/java/com/inmind/pojo/Result.java index a2b7e6e..6a9edb5 100644 --- a/springboot-web-req-resp/src/main/java/com/inmind/pojo/Result.java +++ b/springboot-web-req-resp/src/main/java/com/inmind/pojo/Result.java @@ -46,4 +46,21 @@ public class Result { ", 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; + } }