完善swagger2测试案例

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

@ -1,116 +1,144 @@
package com.ruoyi.web.controller.tool; package com.ruoyi.web.controller.tool;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map;
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.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
/** /**
* swagger * swagger
* *
* @author ruoyi * @author ruoyi
*/ */
@Api("用户信息管理") @Api("用户信息管理")
@RestController @RestController
@RequestMapping("/test/*") @RequestMapping("/test/user")
public class TestController extends BaseController 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")); users.put(1, new UserEntity(1, "admin", "admin123", "15888888888"));
testList.add(new Test("2", "ry", "admin123")); users.put(2, new UserEntity(2, "ry", "admin123", "15666666666"));
} }
@ApiOperation("获取列表") @ApiOperation("获取用户列表")
@GetMapping("list") @GetMapping("/list")
public List<Test> testList() public AjaxResult userList()
{ {
return testList; List<UserEntity> userList = new ArrayList<UserEntity>(users.values());
return AjaxResult.success(userList);
}
@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 AjaxResult.success(users.get(userId));
}
else
{
return error("用户不存在");
}
} }
@ApiOperation("新增用户") @ApiOperation("新增用户")
@PostMapping("save") @ApiImplicitParam(name = "userEntity", value = "新增用户信息", dataType = "UserEntity")
public AjaxResult save(Test test) @PostMapping("/save")
public AjaxResult save(UserEntity user)
{ {
return testList.add(test) ? success() : error(); if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId()))
{
return error("用户ID不能为空");
}
return AjaxResult.success(users.put(user.getUserId(), user));
} }
@ApiOperation("更新用户") @ApiOperation("更新用户")
@ApiImplicitParam(name = "Test", value = "单个用户信息", dataType = "Test") @ApiImplicitParam(name = "userEntity", value = "新增用户信息", dataType = "UserEntity")
@PutMapping("update") @PutMapping("/update")
public AjaxResult update(Test test) public AjaxResult update(UserEntity user)
{ {
return testList.remove(test) && testList.add(test) ? success() : error(); if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId()))
{
return error("用户ID不能为空");
}
if (users.isEmpty() || !users.containsKey(user.getUserId()))
{
return error("用户不存在");
}
users.remove(user.getUserId());
return AjaxResult.success(users.put(user.getUserId(), user));
} }
@ApiOperation("删除用户") @ApiOperation("删除用户信息")
@ApiImplicitParam(name = "Tests", value = "单个用户信息", dataType = "Test") @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "Integer", paramType = "path")
@DeleteMapping("delete") @DeleteMapping("/{userId}")
public AjaxResult delete(Test test) public AjaxResult delete(@PathVariable Integer userId)
{ {
return testList.remove(test) ? success() : error(); if (!users.isEmpty() && users.containsKey(userId))
{
users.remove(userId);
return success();
}
else
{
return error("用户不存在");
}
} }
} }
class Test @ApiModel("用户实体")
class UserEntity
{ {
private String userId; @ApiModelProperty("用户ID")
private Integer userId;
@ApiModelProperty("用户名称")
private String username; private String username;
@ApiModelProperty("用户密码")
private String password; private String password;
public Test() @ApiModelProperty("用户手机")
private String mobile;
public UserEntity()
{ {
} }
public Test(String userId, String username, String password) public UserEntity(Integer userId, String username, String password, String mobile)
{ {
this.userId = userId; this.userId = userId;
this.username = username; this.username = username;
this.password = password; this.password = password;
this.mobile = mobile;
} }
@Override public Integer getUserId()
public boolean equals(Object o)
{
if (this == o)
{
return true;
}
if (o == null || getClass() != o.getClass())
{
return false;
}
Test test = (Test) o;
return userId != null ? userId.equals(test.userId) : test.userId == null;
}
@Override
public int hashCode()
{
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;
}
public String getUserId()
{ {
return userId; return userId;
} }
public void setUserId(String userId) public void setUserId(Integer userId)
{ {
this.userId = userId; this.userId = userId;
} }
@ -134,4 +162,14 @@ class Test
{ {
this.password = password; 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 java.util.HashMap;
import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle; import org.apache.commons.lang3.builder.ToStringStyle;
import com.ruoyi.common.utils.StringUtils;
/** /**
* *
@ -85,7 +86,10 @@ public class AjaxResult extends HashMap<String, Object>
{ {
super.put(CODE_TAG, type.value); super.put(CODE_TAG, type.value);
super.put(MSG_TAG, msg); super.put(MSG_TAG, msg);
super.put(DATA_TAG, data); if (StringUtils.isNotNull(data))
{
super.put(DATA_TAG, data);
}
} }
/** /**
@ -98,6 +102,16 @@ public class AjaxResult extends HashMap<String, Object>
return AjaxResult.success("操作成功"); 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 @Override
public String toString() { public String toString()
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE) {
.append("code", getCode()) return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).append("code", getCode())
.append("msg", getMsg()) .append("msg", getMsg()).append("data", getData()).toString();
.append("data", getData())
.toString();
} }
} }

Loading…
Cancel
Save