• SpringBoot整合Swagger


    手写文档存在的问题

    • 文档需要更新的时候,需要再次发送一份给前端,也就是文档更新交流不及时。
    • 接口返回结果不明确
    • 不能直接在线测试接口,通常需要使用工具,比如:Postman
    • 接口文档太多,不好管理

    使用 Swagger 解决问题

    Swagger 也就是为了解决这个问题,当然也不能说 Swagger 就一定是完美的,当然也有缺点,最明显的就是代码植入性比较强。

    Maven

    增加 Swagger2 所需依赖,pom.xml 配置如下:

    <!-- Swagger2 Begin -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.7.0</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.7.0</version>
    </dependency>
    <!-- Swagger2 End -->
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    配置 Swagger2

    创建一个名为 SwaggerConfig 的 Java 配置类,代码如下:

    package com.manster.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import springfox.documentation.builders.ApiInfoBuilder;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.service.Contact;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    /**
     * @Author manster
     * @Date 2022/3/13
     **/
    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
    
        @Bean
        public Docket webApiConfig(){
            return new Docket(DocumentationType.SWAGGER_2)
                    .groupName("webApi")
                    .apiInfo(webApiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.manster"))
                    .paths(PathSelectors.any())
                    .build();
        }
    
        private ApiInfo webApiInfo(){
            return new ApiInfoBuilder()
                    .title("网站-权限管理API文档")
                    .description("本文档描述了vueadmin权限接口定义")
                    .version("1.0")
                    .contact(new Contact("Manster", "https://gitee.com/manster1231",
                            "1249041911@qq.com"))
                    .build();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    使用 Swagger2

    在 Controller 中增加 Swagger2 相关注解,代码如下:

    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @param tbSysUserJson
     * @return
     */
    @ApiOperation(value = "管理员分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "pageSize", value = "笔数", required = true, dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "tbSysUserJson", value = "管理员对象 JSON 字符串", required = false, dataTypeClass = String.class, paramType = "json")
    })
    @RequestMapping(value = "page/{pageNum}/{pageSize}", method = RequestMethod.GET)
    public BaseResult page(
            @PathVariable(required = true) int pageNum,
            @PathVariable(required = true) int pageSize,
            @RequestParam(required = false) String tbSysUserJson
    ) throws Exception {
    
        TbSysUser tbSysUser = null;
        if (tbSysUserJson != null) {
            tbSysUser = MapperUtils.json2pojo(tbSysUserJson, TbSysUser.class);
        }
        PageInfo pageInfo = adminService.page(pageNum, pageSize, tbSysUser);
    
        // 分页后的结果集
        List<TbSysUser> list = pageInfo.getList();
    
        // 封装 Cursor 对象
        BaseResult.Cursor cursor = new BaseResult.Cursor();
        cursor.setTotal(new Long(pageInfo.getTotal()).intValue());
        cursor.setOffset(pageInfo.getPageNum());
        cursor.setLimit(pageInfo.getPageSize());
    
        return BaseResult.ok(list, cursor);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    Swagger 注解说明

    Swagger 通过注解表明该接口会生成文档,包括接口名、请求方法、参数、返回信息的等等。

    • @Api:修饰整个类,描述 Controller 的作用
    • @ApiOperation:描述一个类的一个方法,或者说一个接口
    • @ApiParam:单个参数描述
    • @ApiModel:用对象来接收参数
    • @ApiProperty:用对象接收参数时,描述对象的一个字段
    • @ApiResponse:HTTP 响应其中 1 个描述
    • @ApiResponses:HTTP 响应整体描述
    • @ApiIgnore:使用该注解忽略这个API
    • @ApiError:发生错误返回的信息
    • @ApiImplicitParam:一个请求参数
    • @ApiImplicitParams:多个请求参数

    访问 Swagger2

    访问地址:http://ip:port/swagger-ui.html

  • 相关阅读:
    vue之组件动态添加style样式的四种写法(齐全)
    M的编程备忘录之Linux——基础开发工具
    算法学习笔记 - 哈希(Hash)
    网络编程——socket定义和地址格式
    51单片机学习:外部中断0实验
    Docker学习笔记
    SpringBoot设置动态定时任务
    QGIS003:【05高级数字化工具栏】-要素移动、修改、合并操作
    WPF 开发,优化 AvalonEdit 显示单行超长文本的性能。
    图片怎么加满屏水印?
  • 原文地址:https://blog.csdn.net/qq_45803593/article/details/125496363