作者 RuoYi

接口使用泛型使其看到响应属性字段

@@ -13,7 +13,7 @@ import org.springframework.web.bind.annotation.RequestBody; @@ -13,7 +13,7 @@ import org.springframework.web.bind.annotation.RequestBody;
13 import org.springframework.web.bind.annotation.RequestMapping; 13 import org.springframework.web.bind.annotation.RequestMapping;
14 import org.springframework.web.bind.annotation.RestController; 14 import org.springframework.web.bind.annotation.RestController;
15 import com.ruoyi.common.core.controller.BaseController; 15 import com.ruoyi.common.core.controller.BaseController;
16 -import com.ruoyi.common.core.domain.AjaxResult; 16 +import com.ruoyi.common.core.domain.R;
17 import com.ruoyi.common.utils.StringUtils; 17 import com.ruoyi.common.utils.StringUtils;
18 import io.swagger.annotations.Api; 18 import io.swagger.annotations.Api;
19 import io.swagger.annotations.ApiImplicitParam; 19 import io.swagger.annotations.ApiImplicitParam;
@@ -40,24 +40,24 @@ public class TestController extends BaseController @@ -40,24 +40,24 @@ public class TestController extends BaseController
40 40
41 @ApiOperation("获取用户列表") 41 @ApiOperation("获取用户列表")
42 @GetMapping("/list") 42 @GetMapping("/list")
43 - public AjaxResult userList() 43 + public R<List<UserEntity>> userList()
44 { 44 {
45 List<UserEntity> userList = new ArrayList<UserEntity>(users.values()); 45 List<UserEntity> userList = new ArrayList<UserEntity>(users.values());
46 - return AjaxResult.success(userList); 46 + return R.ok(userList);
47 } 47 }
48 48
49 @ApiOperation("获取用户详细") 49 @ApiOperation("获取用户详细")
50 @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path", dataTypeClass = Integer.class) 50 @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path", dataTypeClass = Integer.class)
51 @GetMapping("/{userId}") 51 @GetMapping("/{userId}")
52 - public AjaxResult getUser(@PathVariable Integer userId) 52 + public R<UserEntity> getUser(@PathVariable Integer userId)
53 { 53 {
54 if (!users.isEmpty() && users.containsKey(userId)) 54 if (!users.isEmpty() && users.containsKey(userId))
55 { 55 {
56 - return AjaxResult.success(users.get(userId)); 56 + return R.ok(users.get(userId));
57 } 57 }
58 else 58 else
59 { 59 {
60 - return error("用户不存在"); 60 + return R.fail("用户不存在");
61 } 61 }
62 } 62 }
63 63
@@ -69,44 +69,46 @@ public class TestController extends BaseController @@ -69,44 +69,46 @@ public class TestController extends BaseController
69 @ApiImplicitParam(name = "mobile", value = "用户手机", dataType = "String", dataTypeClass = String.class) 69 @ApiImplicitParam(name = "mobile", value = "用户手机", dataType = "String", dataTypeClass = String.class)
70 }) 70 })
71 @PostMapping("/save") 71 @PostMapping("/save")
72 - public AjaxResult save(UserEntity user) 72 + public R<String> save(UserEntity user)
73 { 73 {
74 if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId())) 74 if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId()))
75 { 75 {
76 - return error("用户ID不能为空"); 76 + return R.fail("用户ID不能为空");
77 } 77 }
78 - return AjaxResult.success(users.put(user.getUserId(), user)); 78 + users.put(user.getUserId(), user);
  79 + return R.ok();
79 } 80 }
80 81
81 @ApiOperation("更新用户") 82 @ApiOperation("更新用户")
82 @PutMapping("/update") 83 @PutMapping("/update")
83 - public AjaxResult update(@RequestBody UserEntity user) 84 + public R<String> update(@RequestBody UserEntity user)
84 { 85 {
85 if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId())) 86 if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId()))
86 { 87 {
87 - return error("用户ID不能为空"); 88 + return R.fail("用户ID不能为空");
88 } 89 }
89 if (users.isEmpty() || !users.containsKey(user.getUserId())) 90 if (users.isEmpty() || !users.containsKey(user.getUserId()))
90 { 91 {
91 - return error("用户不存在"); 92 + return R.fail("用户不存在");
92 } 93 }
93 users.remove(user.getUserId()); 94 users.remove(user.getUserId());
94 - return AjaxResult.success(users.put(user.getUserId(), user)); 95 + users.put(user.getUserId(), user);
  96 + return R.ok();
95 } 97 }
96 98
97 @ApiOperation("删除用户信息") 99 @ApiOperation("删除用户信息")
98 @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path", dataTypeClass = Integer.class) 100 @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path", dataTypeClass = Integer.class)
99 @DeleteMapping("/{userId}") 101 @DeleteMapping("/{userId}")
100 - public AjaxResult delete(@PathVariable Integer userId) 102 + public R<String> delete(@PathVariable Integer userId)
101 { 103 {
102 if (!users.isEmpty() && users.containsKey(userId)) 104 if (!users.isEmpty() && users.containsKey(userId))
103 { 105 {
104 users.remove(userId); 106 users.remove(userId);
105 - return success(); 107 + return R.ok();
106 } 108 }
107 else 109 else
108 { 110 {
109 - return error("用户不存在"); 111 + return R.fail("用户不存在");
110 } 112 }
111 } 113 }
112 } 114 }
  1 +package com.ruoyi.common.core.domain;
  2 +
  3 +import java.io.Serializable;
  4 +
  5 +/**
  6 + * 响应信息主体
  7 + *
  8 + * @author ruoyi
  9 + */
  10 +public class R<T> implements Serializable
  11 +{
  12 + private static final long serialVersionUID = 1L;
  13 +
  14 + /** 成功 */
  15 + public static final int SUCCESS = 0;
  16 +
  17 + /** 失败 */
  18 + public static final int FAIL = 500;
  19 +
  20 + private int code;
  21 +
  22 + private String msg;
  23 +
  24 + private T data;
  25 +
  26 + public static <T> R<T> ok()
  27 + {
  28 + return restResult(null, SUCCESS, "操作成功");
  29 + }
  30 +
  31 + public static <T> R<T> ok(T data)
  32 + {
  33 + return restResult(data, SUCCESS, "操作成功");
  34 + }
  35 +
  36 + public static <T> R<T> ok(T data, String msg)
  37 + {
  38 + return restResult(data, SUCCESS, msg);
  39 + }
  40 +
  41 + public static <T> R<T> fail()
  42 + {
  43 + return restResult(null, FAIL, "操作失败");
  44 + }
  45 +
  46 + public static <T> R<T> fail(String msg)
  47 + {
  48 + return restResult(null, FAIL, msg);
  49 + }
  50 +
  51 + public static <T> R<T> fail(T data)
  52 + {
  53 + return restResult(data, FAIL, "操作失败");
  54 + }
  55 +
  56 + public static <T> R<T> fail(T data, String msg)
  57 + {
  58 + return restResult(data, FAIL, msg);
  59 + }
  60 +
  61 + public static <T> R<T> fail(int code, String msg)
  62 + {
  63 + return restResult(null, code, msg);
  64 + }
  65 +
  66 + private static <T> R<T> restResult(T data, int code, String msg)
  67 + {
  68 + R<T> apiResult = new R<>();
  69 + apiResult.setCode(code);
  70 + apiResult.setData(data);
  71 + apiResult.setMsg(msg);
  72 + return apiResult;
  73 + }
  74 +
  75 + public int getCode()
  76 + {
  77 + return code;
  78 + }
  79 +
  80 + public void setCode(int code)
  81 + {
  82 + this.code = code;
  83 + }
  84 +
  85 + public String getMsg()
  86 + {
  87 + return msg;
  88 + }
  89 +
  90 + public void setMsg(String msg)
  91 + {
  92 + this.msg = msg;
  93 + }
  94 +
  95 + public T getData()
  96 + {
  97 + return data;
  98 + }
  99 +
  100 + public void setData(T data)
  101 + {
  102 + this.data = data;
  103 + }
  104 +}