Swagger2实现Restful API 参数图例
为了方便大家学习观看,我分别用了几种不同的方法写,
1.删除用户,代码如下
@ApiOperation(value="删除用户", notes="根据用户的id来删除用户")
@ApiImplicitParam(name = "id", value = "用户ID", required = true,paramType = "query", dataType = "Integer")
@DeleteMapping(value="/deleteUserById")
public String deleteUserById(@RequestParam(value = "id")int id){
User user = userRepository.findById(id);
userRepository.delete(user);
return "success!";
}
2.获取用户详细信息
@ApiOperation(value="获取用户详细信息", notes="根据用户的id来获取用户详细信息")
@ApiImplicitParam(name = "id", value = "用户ID", required = true,paramType = "query", dataType = "Integer")
@GetMapping(value="/findById")
public User findById(@RequestParam(value = "id")int id){
User user = userRepository.findById(id);
return user;
}
3.获取用户列表
@ApiOperation(value="获取用户列表", notes="获取用户列表")
@GetMapping(value="/getUserList")
public List getUserList(){
return userRepository.findAll();
}
4.保存用户
@ApiOperation(value="保存用户", notes="保存用户")
@PostMapping(value="/saveUser")
public String saveUser(@RequestBody @ApiParam(name="用户对象",value="传入json格式",required=true) User user){
userRepository.save(user);
return "success!";
}
5.修改用户
@ApiOperation(value="修改用户", notes="修改用户")
@ApiImplicitParams({
@ApiImplicitParam(name="id",value="主键id",required=true,paramType="query",dataType="Integer"),
@ApiImplicitParam(name="username",value="用户名称",required=true,paramType="query",dataType = "String"),
@ApiImplicitParam(name="password",value="用户密码",required=true,paramType="query",dataType = "String")
})
@PutMapping(value="/updateUser")
public String updateUser(@RequestParam(value = "id")int id,@RequestParam(value = "username")String username,
@RequestParam(value = "password")String password){
User user = new User(id, username, password);
userRepository.save(user);
return "success!";
}