From 9a83cc02331fb58b805ea14acac262825be2c27e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98=E5=B0=8F=E5=A2=A8?= <2291200076@qq.com> Date: Tue, 1 Mar 2022 17:57:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9A=E4=B9=89=E9=80=9A=E7=94=A8=E7=9A=84?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E5=AF=B9=E8=B1=A1--=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E6=AD=A3=E7=A1=AE=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 ++- .../flashsale/controller/UserController.java | 9 ++++- .../flashsale/response/CommonReturnType.java | 38 +++++++++++++++++++ 3 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/cxyxiaomo/flashsale/response/CommonReturnType.java diff --git a/README.md b/README.md index 0725429..52a62da 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ 预期效果: ```json -{"id":1,"name":"admin","gender":2,"age":18,"telephone":"110"} +{"data":{"id":1,"name":"admin","gender":2,"age":18,"telephone":"110"},"status":"success"} ``` ### 层次结构(以User为例,自上向下) @@ -40,4 +40,6 @@ | Mapping | resources/mapping | Mapper接口实现类 | xml格式;SQL语句 | mapping/UserDOMapper.xml | | Data Object | dataobject | 类 class | | dataobject.UserDO | -**Tips:** Model与Data Object并非完全一一对应,例如UserModel是由ServiceImpl将UserDO和UserPasswordDO组装而成的。 \ No newline at end of file +**Tips:** Model与Data Object并非完全一一对应,例如UserModel是由ServiceImpl将UserDO和UserPasswordDO组装而成的。 + +response: 用于处理HTTP请求返回 \ No newline at end of file diff --git a/src/main/java/com/cxyxiaomo/flashsale/controller/UserController.java b/src/main/java/com/cxyxiaomo/flashsale/controller/UserController.java index 1034d81..ac0e7e7 100644 --- a/src/main/java/com/cxyxiaomo/flashsale/controller/UserController.java +++ b/src/main/java/com/cxyxiaomo/flashsale/controller/UserController.java @@ -1,6 +1,7 @@ package com.cxyxiaomo.flashsale.controller; import com.cxyxiaomo.flashsale.controller.viewobject.UserVO; +import com.cxyxiaomo.flashsale.response.CommonReturnType; import com.cxyxiaomo.flashsale.service.UserService; import com.cxyxiaomo.flashsale.service.model.UserModel; import org.apache.catalina.User; @@ -20,11 +21,15 @@ public class UserController { @RequestMapping("/get") @ResponseBody - public UserVO getUser(@RequestParam(name = "id") Integer id) { + public CommonReturnType getUser(@RequestParam(name = "id") Integer id) { // 调用Services服务获取对应id的用户对象并返回给前端 UserModel userModel = userService.getUserById(id); // 将核心领域模型用户对象转化为可供UI使用的viewobject - return convertFromModel(userModel); + UserVO userVO = convertFromModel(userModel); + + // 返回通用对象 + CommonReturnType commonReturnType = CommonReturnType.create(userVO); + return commonReturnType; } private UserVO convertFromModel(UserModel userModel) { diff --git a/src/main/java/com/cxyxiaomo/flashsale/response/CommonReturnType.java b/src/main/java/com/cxyxiaomo/flashsale/response/CommonReturnType.java new file mode 100644 index 0000000..ea633da --- /dev/null +++ b/src/main/java/com/cxyxiaomo/flashsale/response/CommonReturnType.java @@ -0,0 +1,38 @@ +package com.cxyxiaomo.flashsale.response; + +public class CommonReturnType { + // 表明对应请求的返回处理结果 "success" 或 "fail" + private String Status; + + // 若 status == "success" 则data内返回前端需要的JSON数据 + // 若 status == "fail" 则data内使用通用的错误码格式 + private Object Data; + + // 定义一个通用的创建方法 + public static CommonReturnType create(Object result) { + return CommonReturnType.create(result, "success"); + } + + public static CommonReturnType create(Object result, String status) { + CommonReturnType type = new CommonReturnType(); + type.setStatus(status); + type.setData(result); + return type; + } + + public String getStatus() { + return Status; + } + + public void setStatus(String status) { + Status = status; + } + + public Object getData() { + return Data; + } + + public void setData(Object data) { + Data = data; + } +}