写了7个接口,Swagger中只显示4个
代码实现的内容是Controller层的关于用户CRUD操作,使用REST风格进行编写。
REST是一种组织Web服务的架构,访问资源时,使用行为动词(GET、POST、PUT、DELETE)来区分对资源进行的增删改查操作。
package com.atguigu.auth.controller;
import com.atguigu.common.result.Result;
import com.atguigu.model.system.SysRole;
import com.atguigu.vo.system.SysRoleQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.atguigu.auth.service.sysRoleService;
import java.security.PublicKey;
import java.util.List;
@Api(tags = "角色管理的接口")
@RestController
@RequestMapping("admin/system/sysRole")
public class SysRoleController {
@Autowired
private sysRoleService sysRoleService;
// 封装了统一返回结果
@ApiOperation("查询所有的角色")
@GetMapping("/findAll")
public Result findAll() {
List<SysRole> roleList = sysRoleService.list();
return Result.ok(roleList);
}
// 条件分页查询
@ApiOperation("条件分页查询")
@GetMapping("/{page}/{limit}")
public Result pageQueryRole(@PathVariable Long page,
@PathVariable Long limit,
SysRoleQueryVo sysRoleQueryVo) {
// 分页查询相关参数
Page<SysRole> pageParam = new Page<>(page, limit);
// MP的lamda条件语句的分页查询条件
LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
String roleName = sysRoleQueryVo.getRoleName();
if (!StringUtils.isEmpty(roleName)) {
wrapper.like(SysRole::getRoleName,roleName);
}
// 调用servic方法实现
IPage<SysRole> pageModel = sysRoleService.page(pageParam, wrapper);
return Result.ok(pageModel);
}
@ApiOperation("根据id获取查询")
@GetMapping("get/{id}")
public Result get(@PathVariable Long id) {
SysRole roleById = sysRoleService.getById(id);
return Result.ok(roleById);
}
@ApiOperation("新增角色")
@PostMapping("save")
// 通过请求体进行传递
public Result save(@RequestBody SysRole sysRole) {
boolean save = sysRoleService.save(sysRole);
if (save == true){
return Result.ok();
}else {
return Result.fail();
}
}
@ApiOperation("根据ID修改角色")
@PutMapping("update")
// 根据ID查询出角色 然后修改角色 (Q:在哪里传入了id呢?)
public Result update (@RequestBody @Validated SysRole sysRole) {
boolean b = sysRoleService.updateById(sysRole);
if (b == true){
return Result.ok();
}else {
return Result.fail();
}
}
@ApiOperation("根据ID删除角色")
@DeleteMapping("remove/{id}")
public Result remove (@PathVariable Long id) {
boolean b = sysRoleService.removeById(id);
if (b == true){
return Result.ok();
}else {
return Result.fail();
}
}
// 前端使用json数组格式传递 后端使用List集合接受
@ApiOperation("批量删除")
@DeleteMapping("batchRemove")
public Result batchRemove (@RequestBody List<Long> idList){
boolean b = sysRoleService.removeByIds(idList);
if (b == true){
return Result.ok();
}else {
return Result.fail();
}
}
}
不知道为什么写了七个接口左侧只显示了四个
观察后发现有@RequestBody 的三个接口没有显示出来
可能是swagger和自定义参数解析器的功能冲突 参见https://blog.youkuaiyun.com/wdj_yyds/article/details/122351523?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522170471682516800185899178%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257D&request_id=170471682516800185899178&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduend~default-2-122351523-null-null.142v99pc_search_result_base7&utm_term=swagger%20%40requestbody&spm=1018.2226.3001.4187
删掉@RequestBody之后就好了,然后接口测试也成功。