• 前置条件

    在开发之前,请确保

    介绍

    demo 需涉及 api 层的 controller

    编写 Controller

    编写 SwaggerApiConfig

    .1 SwaggerApiConfig.java 代码

    package org.hzero.todo.config;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Configuration;
    import springfox.documentation.service.Tag;
    import springfox.documentation.spring.web.plugins.Docket;
    
    /**
     * Swagger Api 描述配置
     */
    @Configuration
    public class SwaggerApiConfig {
        public static final String USER = "User";
        public static final String TASK = "Task";
    
    
        @Autowired
        public SwaggerApiConfig(Docket docket) {
            docket.tags(
                    new Tag(USER, "用户信息"),
                    new Tag(TASK, "任务信息")
            );
        }
    }
    

    Controller 类相关标签

    .1 UserController.java代码

    package org.hzero.todo.api.controller.v1;
    
    import ....;
    
    /**
     * 用户接口
     */
    @Api(tags = SwaggerApiConfig.USER)
    @RestController("userController.v1")
    @RequestMapping("/v1/users")
    public class UserController extends BaseController {
    
        private final UserService userService;
        private final UserRepository userRepository;
    
        public UserController(UserService userService, UserRepository userRepository) {
            this.userService = userService;
            this.userRepository = userRepository;
        }
    
        @Permission(level = ResourceLevel.SITE)
        @ApiOperation(value = "分页查询用户")
        @GetMapping
        public ResponseEntity<Page<User>> pageTask(User user, PageRequest pageRequest) {
            return Results.success(userRepository.pageAndSort(pageRequest, user));
        }
    
        @Permission(level = ResourceLevel.SITE)
        @ApiOperation(value = "创建 todo 用户")
        @PostMapping
        private ResponseEntity<User> createUser(@RequestBody User user) {
            // 简单数据校验
            this.validObject(user);
            // 创建用户
            return Results.success(userService.create(user));
        }
    
        @Permission(level = ResourceLevel.SITE)
        @ApiOperation(value = "删除 todo 用户")
        @DeleteMapping
        private ResponseEntity<User> deleteUser(@RequestBody User user) {
            // 数据防篡改校验
            SecurityTokenHelper.validToken(user);
            // 删除用户
            userService.delete(user.getId());
            return Results.success();
        }
    }
    

    .2 TaskController.java代码

    package org.hzero.todo.api.controller.v1;
    
    import ....;
    
    /**
     * 任务接口(全是租户级接口)
     */
    @Api(tags = SwaggerApiConfig.TASK)
    @RestController("taskController.v1")
    @RequestMapping("/v1/{organizationId}/tasks")
    public class TaskController extends BaseController {
    
        private final TaskService taskService;
        private final TaskRepository taskRepository;
    
        public TaskController(TaskService taskService, TaskRepository taskRepository) {
            this.taskService = taskService;
            this.taskRepository = taskRepository;
        }
    
        /**
         * 注意分页参数是 io.choerodon.mybatis.pagehelper.domain.PageRequest;
         */
        @Permission(level = ResourceLevel.ORGANIZATION)
        @ApiOperation(value = "根据taskNumber分页查询task")
        @GetMapping
        public ResponseEntity<Page<Task>> pageTask(@PathVariable("organizationId") Long tenantId, Task task, PageRequest pageRequest) {
            task.setTenantId(tenantId);
            return Results.success(taskRepository.pageTask(task, pageRequest));
        }
    
        @Permission(level = ResourceLevel.ORGANIZATION)
        @ApiOperation(value = "创建task")
        @PostMapping
        public ResponseEntity<Task> create(@PathVariable("organizationId") Long tenantId, @RequestBody Task task) {
            task.setTenantId(tenantId);
            // 简单数据校验
            this.validObject(task);
            return Results.success(taskService.create(task));
        }
    
        @Permission(level = ResourceLevel.ORGANIZATION)
        @ApiOperation(value = "更新task")
        @PutMapping
        public ResponseEntity<Task> update(@PathVariable("organizationId") Long tenantId, @RequestBody Task task) {
            // 简单数据校验
            this.validObject(task);
            // 数据防篡改校验
            SecurityTokenHelper.validToken(task);
            return Results.success(taskService.update(task));
        }
    
        @Permission(level = ResourceLevel.ORGANIZATION)
        @ApiOperation(value = "根据taskNumber查询task")
        @ApiImplicitParams({
                @ApiImplicitParam(value = "任务编号", paramType = "string")
        })
        @GetMapping("/{taskNumber}")
        public ResponseEntity<Task> selectDetail(@PathVariable Long organizationId, @PathVariable String taskNumber) {
            return Results.success(taskRepository.selectDetailByTaskNumber(taskNumber));
        }
    
        @Permission(level = ResourceLevel.ORGANIZATION)
        @ApiOperation(value = "根据taskNumber删除task")
        @DeleteMapping("/{taskNumber}")
        public void deleteByTaskNumber(@PathVariable Long organizationId, @PathVariable @ApiParam(value = "任务编号") String taskNumber) {
            taskService.deleteByTaskNumber(taskNumber);
        }
    }
    

    编写DTO