完善swagger2测试案例

master
RuoYi 6 years ago committed by Limy
parent 2728a72a96
commit 29a4529a6b

@ -1,116 +1,144 @@
package com.ruoyi.web.controller.tool;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
/**
* swagger
* swagger
*
* @author ruoyi
*/
@Api("用户信息管理")
@RestController
@RequestMapping("/test/*")
@RequestMapping("/test/user")
public class TestController extends BaseController
{
private final static List<Test> testList = new ArrayList<>();
private final static Map<Integer, UserEntity> users = new LinkedHashMap<Integer, UserEntity>();
{
testList.add(new Test("1", "admin", "admin123"));
testList.add(new Test("2", "ry", "admin123"));
users.put(1, new UserEntity(1, "admin", "admin123", "15888888888"));
users.put(2, new UserEntity(2, "ry", "admin123", "15666666666"));
}
@ApiOperation("获取列表")
@GetMapping("list")
public List<Test> testList()
@ApiOperation("获取用户列表")
@GetMapping("/list")
public AjaxResult userList()
{
return testList;
List<UserEntity> userList = new ArrayList<UserEntity>(users.values());
return AjaxResult.success(userList);
}
@ApiOperation("新增用户")
@PostMapping("save")
public AjaxResult save(Test test)
@ApiOperation("获取用户详细")
@ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "Integer", paramType = "path")
@GetMapping("/{userId}")
public AjaxResult getUser(@PathVariable Integer userId)
{
if (!users.isEmpty() && users.containsKey(userId))
{
return testList.add(test) ? success() : error();
return AjaxResult.success(users.get(userId));
}
@ApiOperation("更新用户")
@ApiImplicitParam(name = "Test", value = "单个用户信息", dataType = "Test")
@PutMapping("update")
public AjaxResult update(Test test)
else
{
return testList.remove(test) && testList.add(test) ? success() : error();
return error("用户不存在");
}
}
@ApiOperation("删除用户")
@ApiImplicitParam(name = "Tests", value = "单个用户信息", dataType = "Test")
@DeleteMapping("delete")
public AjaxResult delete(Test test)
@ApiOperation("新增用户")
@ApiImplicitParam(name = "userEntity", value = "新增用户信息", dataType = "UserEntity")
@PostMapping("/save")
public AjaxResult save(UserEntity user)
{
if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId()))
{
return testList.remove(test) ? success() : error();
return error("用户ID不能为空");
}
return AjaxResult.success(users.put(user.getUserId(), user));
}
class Test
@ApiOperation("更新用户")
@ApiImplicitParam(name = "userEntity", value = "新增用户信息", dataType = "UserEntity")
@PutMapping("/update")
public AjaxResult update(UserEntity user)
{
private String userId;
private String username;
private String password;
public Test()
if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId()))
{
return error("用户ID不能为空");
}
public Test(String userId, String username, String password)
if (users.isEmpty() || !users.containsKey(user.getUserId()))
{
this.userId = userId;
this.username = username;
this.password = password;
return error("用户不存在");
}
users.remove(user.getUserId());
return AjaxResult.success(users.put(user.getUserId(), user));
}
@Override
public boolean equals(Object o)
@ApiOperation("删除用户信息")
@ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "Integer", paramType = "path")
@DeleteMapping("/{userId}")
public AjaxResult delete(@PathVariable Integer userId)
{
if (this == o)
if (!users.isEmpty() && users.containsKey(userId))
{
return true;
users.remove(userId);
return success();
}
if (o == null || getClass() != o.getClass())
else
{
return false;
return error("用户不存在");
}
}
}
@ApiModel("用户实体")
class UserEntity
{
@ApiModelProperty("用户ID")
private Integer userId;
@ApiModelProperty("用户名称")
private String username;
Test test = (Test) o;
@ApiModelProperty("用户密码")
private String password;
@ApiModelProperty("用户手机")
private String mobile;
public UserEntity()
{
return userId != null ? userId.equals(test.userId) : test.userId == null;
}
@Override
public int hashCode()
public UserEntity(Integer userId, String username, String password, String mobile)
{
int result = userId != null ? userId.hashCode() : 0;
result = 31 * result + (username != null ? username.hashCode() : 0);
result = 31 * result + (password != null ? password.hashCode() : 0);
return result;
this.userId = userId;
this.username = username;
this.password = password;
this.mobile = mobile;
}
public String getUserId()
public Integer getUserId()
{
return userId;
}
public void setUserId(String userId)
public void setUserId(Integer userId)
{
this.userId = userId;
}
@ -134,4 +162,14 @@ class Test
{
this.password = password;
}
public String getMobile()
{
return mobile;
}
public void setMobile(String mobile)
{
this.mobile = mobile;
}
}

@ -3,6 +3,7 @@ package com.ruoyi.common.core.domain;
import java.util.HashMap;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.ruoyi.common.utils.StringUtils;
/**
*
@ -85,8 +86,11 @@ public class AjaxResult extends HashMap<String, Object>
{
super.put(CODE_TAG, type.value);
super.put(MSG_TAG, msg);
if (StringUtils.isNotNull(data))
{
super.put(DATA_TAG, data);
}
}
/**
*
@ -98,6 +102,16 @@ public class AjaxResult extends HashMap<String, Object>
return AjaxResult.success("操作成功");
}
/**
*
*
* @return
*/
public static AjaxResult success(Object data)
{
return AjaxResult.success("操作成功", data);
}
/**
*
*
@ -218,11 +232,9 @@ public class AjaxResult extends HashMap<String, Object>
}
@Override
public String toString() {
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
.append("code", getCode())
.append("msg", getMsg())
.append("data", getData())
.toString();
public String toString()
{
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).append("code", getCode())
.append("msg", getMsg()).append("data", getData()).toString();
}
}

Loading…
Cancel
Save