From ff46b496244b7a182bb72ba837025ff6204baf61 Mon Sep 17 00:00:00 2001 From: xuxin <840198532@qq.com> Date: Sun, 17 May 2026 11:16:30 +0800 Subject: [PATCH] =?UTF-8?q?javaEEday05-=E8=AF=B7=E6=B1=82=E5=93=8D?= =?UTF-8?q?=E5=BA=94-=E5=93=8D=E5=BA=94-@ResponseBody&=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E5=93=8D=E5=BA=94=E7=BB=93=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../inmind/controller/ResponseController.java | 33 +++++++++++ .../src/main/java/com/inmind/pojo/Result.java | 55 +++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 springboot-web-req-resp/src/main/java/com/inmind/controller/ResponseController.java create mode 100644 springboot-web-req-resp/src/main/java/com/inmind/pojo/Result.java 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 new file mode 100644 index 0000000..8403d4a --- /dev/null +++ b/springboot-web-req-resp/src/main/java/com/inmind/controller/ResponseController.java @@ -0,0 +1,33 @@ +package com.inmind.controller; + +import com.inmind.pojo.Address; +import com.inmind.pojo.Result; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +public class ResponseController { + + //响应字符串 + @RequestMapping("/hello") + public Result hello(){ + System.out.println("hello world"); + return Result.success("hello world"); + } + + //响应对象 + @RequestMapping("/getAddress") + public Result getAddress(){ + Address address = new Address("北京", "北京"); + return Result.success(address); + } + //响应集合 + @RequestMapping("/listAddress") + public Result listAddress(){ + Address address1 = new Address("北京", "北京"); + Address address2 = new Address("江苏", "常州"); + return Result.success(List.of(address1,address2)); + } +} 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 new file mode 100644 index 0000000..2b7c9ce --- /dev/null +++ b/springboot-web-req-resp/src/main/java/com/inmind/pojo/Result.java @@ -0,0 +1,55 @@ +package com.inmind.pojo; + +/** + * 统一响应结果封装类 + */ +public class Result { + private Integer code ;//1 成功 , 0 失败 + private String msg; //提示信息 + private Object data; //数据 date + + public Result() { + } + public Result(Integer code, String msg, Object data) { + this.code = code; + this.msg = msg; + this.data = data; + } + public Integer getCode() { + return code; + } + public void setCode(Integer code) { + this.code = code; + } + public String getMsg() { + return msg; + } + public void setMsg(String msg) { + this.msg = msg; + } + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + + public static Result success(Object data){ + return new Result(1, "success", data); + } + public static Result success(){ + return new Result(1, "success", null); + } + public static Result error(String msg){ + return new Result(0, msg, null); + } + + @Override + public String toString() { + return "Result{" + + "code=" + code + + ", msg='" + msg + '\'' + + ", data=" + data + + '}'; + } +}