添加支持泛型的返回对象,并修改TestController实例
This commit is contained in:
parent
6c32fddc7b
commit
f5553e2bca
|
|
@ -4,6 +4,8 @@ import java.util.ArrayList;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.ruoyi.common.core.domain.XaResult;
|
||||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
|
@ -33,6 +35,7 @@ import io.swagger.annotations.ApiOperation;
|
||||||
public class TestController extends BaseController
|
public class TestController extends BaseController
|
||||||
{
|
{
|
||||||
private final static Map<Integer, UserEntity> users = new LinkedHashMap<Integer, UserEntity>();
|
private final static Map<Integer, UserEntity> users = new LinkedHashMap<Integer, UserEntity>();
|
||||||
|
|
||||||
{
|
{
|
||||||
users.put(1, new UserEntity(1, "admin", "admin123", "15888888888"));
|
users.put(1, new UserEntity(1, "admin", "admin123", "15888888888"));
|
||||||
users.put(2, new UserEntity(2, "ry", "admin123", "15666666666"));
|
users.put(2, new UserEntity(2, "ry", "admin123", "15666666666"));
|
||||||
|
|
@ -40,24 +43,24 @@ public class TestController extends BaseController
|
||||||
|
|
||||||
@ApiOperation("获取用户列表")
|
@ApiOperation("获取用户列表")
|
||||||
@GetMapping("/list")
|
@GetMapping("/list")
|
||||||
public AjaxResult userList()
|
public XaResult<List<UserEntity>> userList()
|
||||||
{
|
{
|
||||||
List<UserEntity> userList = new ArrayList<UserEntity>(users.values());
|
List<UserEntity> userList = new ArrayList<UserEntity>(users.values());
|
||||||
return AjaxResult.success(userList);
|
return XaResult.success(userList);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiOperation("获取用户详细")
|
@ApiOperation("获取用户详细")
|
||||||
@ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path")
|
@ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path")
|
||||||
@GetMapping("/{userId}")
|
@GetMapping("/{userId}")
|
||||||
public AjaxResult getUser(@PathVariable Integer userId)
|
public XaResult<UserEntity> getUser(@PathVariable Integer userId)
|
||||||
{
|
{
|
||||||
if (!users.isEmpty() && users.containsKey(userId))
|
if (!users.isEmpty() && users.containsKey(userId))
|
||||||
{
|
{
|
||||||
return AjaxResult.success(users.get(userId));
|
return XaResult.success(users.get(userId));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return error("用户不存在");
|
return XaResult.error("用户不存在");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -69,44 +72,44 @@ public class TestController extends BaseController
|
||||||
@ApiImplicitParam(name = "mobile", value = "用户手机", dataType = "String")
|
@ApiImplicitParam(name = "mobile", value = "用户手机", dataType = "String")
|
||||||
})
|
})
|
||||||
@PostMapping("/save")
|
@PostMapping("/save")
|
||||||
public AjaxResult save(UserEntity user)
|
public XaResult<UserEntity> save(UserEntity user)
|
||||||
{
|
{
|
||||||
if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId()))
|
if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId()))
|
||||||
{
|
{
|
||||||
return error("用户ID不能为空");
|
return XaResult.error("用户ID不能为空");
|
||||||
}
|
}
|
||||||
return AjaxResult.success(users.put(user.getUserId(), user));
|
return XaResult.success(users.put(user.getUserId(), user));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiOperation("更新用户")
|
@ApiOperation("更新用户")
|
||||||
@PutMapping("/update")
|
@PutMapping("/update")
|
||||||
public AjaxResult update(@RequestBody UserEntity user)
|
public XaResult<UserEntity> update(@RequestBody UserEntity user)
|
||||||
{
|
{
|
||||||
if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId()))
|
if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId()))
|
||||||
{
|
{
|
||||||
return error("用户ID不能为空");
|
return XaResult.error("用户ID不能为空");
|
||||||
}
|
}
|
||||||
if (users.isEmpty() || !users.containsKey(user.getUserId()))
|
if (users.isEmpty() || !users.containsKey(user.getUserId()))
|
||||||
{
|
{
|
||||||
return error("用户不存在");
|
return XaResult.error("用户不存在");
|
||||||
}
|
}
|
||||||
users.remove(user.getUserId());
|
users.remove(user.getUserId());
|
||||||
return AjaxResult.success(users.put(user.getUserId(), user));
|
return XaResult.success(users.put(user.getUserId(), user));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiOperation("删除用户信息")
|
@ApiOperation("删除用户信息")
|
||||||
@ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path")
|
@ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path")
|
||||||
@DeleteMapping("/{userId}")
|
@DeleteMapping("/{userId}")
|
||||||
public AjaxResult delete(@PathVariable Integer userId)
|
public XaResult<UserEntity> delete(@PathVariable Integer userId)
|
||||||
{
|
{
|
||||||
if (!users.isEmpty() && users.containsKey(userId))
|
if (!users.isEmpty() && users.containsKey(userId))
|
||||||
{
|
{
|
||||||
users.remove(userId);
|
users.remove(userId);
|
||||||
return success();
|
return XaResult.success();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return error("用户不存在");
|
return XaResult.error("用户不存在");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,129 @@
|
||||||
|
package com.ruoyi.common.core.domain;
|
||||||
|
|
||||||
|
import io.swagger.annotations.ApiModel;
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author eason.zt
|
||||||
|
* @version V1.0
|
||||||
|
* @Title: XaResult.java
|
||||||
|
* @Package com.web.hhrz.base.kuadi100
|
||||||
|
* @Description: Service返回结果统一对象
|
||||||
|
* @date 2014年8月13日 下午7:46:23
|
||||||
|
* @update 2021年7月19号
|
||||||
|
* @updateBy 哦是吗
|
||||||
|
*/
|
||||||
|
@ApiModel
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public class XaResult<T> {
|
||||||
|
|
||||||
|
@ApiModelProperty(value = "code : 返回代码,0表示OK,其它的都有对应问题",position = 1)
|
||||||
|
private int code = 0;
|
||||||
|
|
||||||
|
@ApiModelProperty(value = "msg : 如果code!=0,错误信息",position = 2)
|
||||||
|
private String msg;
|
||||||
|
|
||||||
|
@ApiModelProperty(value = "如果code!=0,message的补充信息",position = 3)
|
||||||
|
private Object exception;
|
||||||
|
|
||||||
|
@ApiModelProperty(value = "code为0时返回结果集",position = 4)
|
||||||
|
private T data;
|
||||||
|
|
||||||
|
public int getCode() {
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCode(int code) {
|
||||||
|
this.code = code;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMsg() {
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMsg(String msg) {
|
||||||
|
this.msg = msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object getException() {
|
||||||
|
return exception;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setException(Object exception) {
|
||||||
|
this.exception = exception;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T getData() {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public XaResult<T> setData(T data) {
|
||||||
|
this.data = data;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public XaResult(String errorMsg) {
|
||||||
|
this.msg = errorMsg;
|
||||||
|
this.code = 500;
|
||||||
|
this.data = (T) new Object();
|
||||||
|
}
|
||||||
|
|
||||||
|
public XaResult(String errorMsg, int code) {
|
||||||
|
this.msg = errorMsg;
|
||||||
|
this.code = code;
|
||||||
|
this.data = (T) new Object();
|
||||||
|
}
|
||||||
|
|
||||||
|
public XaResult(T data) {
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public XaResult() {
|
||||||
|
this.data = (T) new Object();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> XaResult<T> success(T object) {
|
||||||
|
XaResult<T> xaResult = new XaResult<>();
|
||||||
|
xaResult.setCode(0);
|
||||||
|
xaResult.setMsg("success");
|
||||||
|
xaResult.setData(object);
|
||||||
|
return xaResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> XaResult<T> success() {
|
||||||
|
XaResult<T> xaResult = new XaResult<>();
|
||||||
|
xaResult.setCode(0);
|
||||||
|
xaResult.setMsg("success");
|
||||||
|
xaResult.setData(null);
|
||||||
|
return xaResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> XaResult<T> error(String msg, T object) {
|
||||||
|
XaResult<T> xaResult = new XaResult<>();
|
||||||
|
xaResult.setCode(500);
|
||||||
|
xaResult.setMsg(msg);
|
||||||
|
xaResult.setData(object);
|
||||||
|
return xaResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static <T> XaResult<T> error(String msg) {
|
||||||
|
XaResult<T> xaResult = new XaResult<>();
|
||||||
|
xaResult.setCode(500);
|
||||||
|
xaResult.setMsg(msg);
|
||||||
|
xaResult.setData(null);
|
||||||
|
return xaResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static <T> XaResult<T> error() {
|
||||||
|
XaResult<T> xaResult = new XaResult<>();
|
||||||
|
xaResult.setCode(500);
|
||||||
|
xaResult.setMsg("未知异常,请联系管理员");
|
||||||
|
xaResult.setData(null);
|
||||||
|
return xaResult;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue