mirror of
				https://gitee.com/coder-xiaomo/flashsale
				synced 2025-11-04 14:13:14 +08:00 
			
		
		
		
	定义通用的返回对象--返回正确信息
This commit is contained in:
		@@ -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) {
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user