设为首页 加入收藏

TOP

Spring Boot 中使用 Swagger(一)
2023-07-25 21:31:11 】 浏览:44
Tags:Spring Boot Swagger

前后端分离开发,后端需要编写接?说明?档,会耗费?较多的时间。
swagger 是?个?于?成服务器接?的规范性?档,并且能够对接?进?测试的?具。

作用

  • ?成接?说明?档
  • 对接?进?测试

使用步骤

  1. 添加依赖

    <!--swagger-->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.9.2</version>
    </dependency>
    
  2. 写配置类 SwaggerConfig

    /**
     * SwaggerConfig 接口文档配置类
     */
    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
    
        /**
         * 配置接口文档生成规则
         */
        @Bean
        public Docket getDocket() {
            // 设置文档生成规则
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo()) // 设置文档信息
                    .select()
                    // 设置哪个包下的类需要生成文档
                   .apis(RequestHandlerSelectors.basePackage("com.luis.fmmall.controller"))
                    .paths(PathSelectors.any()) // 定义哪些路径的接口需要生成文档
                    .build();
    
        }
    
        /**
         * 设置文档信息
         */
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("xxx接口文档")
                    .description("这里是相关描述")
                    .version("1.0")
                    .contact(new Contact("luis",
                            "https://www.cnblogs.com/luisblog",
                            "xxx@qq.com"))
                    .build();
        }
    }
    
  3. 在控制器类上使用 Swagger 的注解进行相关说明

    示例如下:

    @RestController
    @RequestMapping("/user")
    @Api(tags = "用户管理", value = "提供用户的登陆、注册、修改等功能") //类说明
    public class UserController {
    
        @Resource
        private UserService userService;
    
        @GetMapping("/login")
        @ApiOperation(value = "登陆验证", notes = "用户登陆检查") //方法名说明
        @ApiImplicitParams({ //参数说明
                @ApiImplicitParam(dataType = "string", name = "username", value = "用户名", required = true),
                @ApiImplicitParam(dataType = "string", name = "password", value = "用户密码", required = false, defaultValue = "123")
        })
        public ResultVo login(@RequestParam("username") String name,
                              @RequestParam(value = "password", defaultValue = "123") String pwd) {
            return userService.checkLogin(name, pwd);
        }
    }
    
  4. 启动 SpringBoot 应用,访问 http://localhost:8080/swagger-ui.html

    效果如下:

    image-20221105192723545

常用注解说明

  • @Api:类注解,使用在控制器类上,对类进行说明

    控制器类 UserController 示例:

    @Api(tags = "用户管理", value = "提供用户的登陆、注册、修改等功能") //类说明
    public class UserController {
    }
    
  • @ApiOperation:方法注解,使用在方法上,对方法名进行说明

  • @ApiImplicitParam@ApiImplicitParams:方法注解,使用在方法上,对方法的形参进行说明

    单个形参使用 @ApiImplicitParam,多个形参使用 @ApiImplicitParams

    控制器类 UserController 的 login 方法示例:

    @GetMapping("/login")
    @ApiOperation(value = "登陆验证", notes = "用户登陆检查") //方法名说明
    @ApiImplicitParams({ //参数说明
        @ApiImplicitParam(dataType = "string", name = "username", value = "用户名", required = true),
        @ApiImplicitParam(dataType = "string", name = "password", value = "用户密码", required = false, defaultValue = "123")
    })
    public ResultVo login(@RequestParam("username") String name,
                          @RequestParam(value = "password", defaultValue = "123") String pwd) {
        return userService.checkLogin(name, pwd);
    }
    
  • @ApiModel @ApiModelProperty:当接?的形参或返回值为对象类型时,在实体类中添加此注解说明

    接口的返回值为 ResultVo 对象示例:

    @Da
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇学习笔记——元数据、blob类型的.. 下一篇Optional用法与争议点

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目