• swagger---接口文档管理生成管理工具


    Swagger–接口生成工具

    使用Swagger你只需要按照它的规范去定义接口及接口相关的信息,再通过Swagger衍生出来的一系列项目和工具,
    就可以做到生成各种格式的接口文档,以及在线接口调试页面等等。

    • 官网: https://lswagger.io/
    • knife4j是为Java MVC框架集成Swagger生成Api文档的增强解决方案。

    swagger的使用

    操作步骤:
    1、导入knife4j的maven坐标

    <dependency>
        <groupId>com.github.xiaoymingroupId>
        <artifactId>knife4j-spring-boot-starterartifactId>
        <version>3.0.2version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2、导入knife4j相关配置类

    package com.mannor.reggie_take_out.config;
    
    import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
    import com.mannor.reggie_take_out.common.JacksonObjectMapper;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
    import springfox.documentation.builders.ApiInfoBuilder;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    
    
    @Slf4j
    @Configuration
    @EnableSwagger2
    @EnableKnife4j
    public class WebMvcConfig extends WebMvcConfigurationSupport {
    
        /**
         * 设置静态资源映射
         * @param registry
         */
        @Override
        protected void addResourceHandlers(ResourceHandlerRegistry registry) {
            log.info("开始进行静态资源映射...");
            registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
            registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
            registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/");
            registry.addResourceHandler("/front/**").addResourceLocations("classpath:/front/");
        }
    
        @Bean
        public Docket createRestApi() {
            // 文档类型
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.mannor.reggie_take_out.controller"))
                    .paths(PathSelectors.any())
                    .build();
        }
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("瑞吉外卖")
                    .version("1.0")
                    .description("瑞吉外卖接口文档")
                    .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
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    3、设置静态资源,否则接口文档页面无法访问

    image-20230906192337566

    第二步的配置类上有此代码。

    4、在LoginCheckFilter中设置不需要处理的请求路径

    image-20230906192811818

    swagger启动

    将你的项目启动,然后在你的浏览器打开doc.html文件,即可进行文档的管理:

    image-20230906200351744

  • 相关阅读:
    Gitlab: PHP项目CI/CD实践
    安装zabbix-agent监控linux
    [Nacos][Rancher][微服务] 容器化Docker部署的Nacos拒接连接
    构建全面 AI Agent 解决方案:Chocolate Factory 框架的文本到 UI、图表和测试用例生成...
    C#:实现无向图的最小生成树的算法(附完整源码)
    Linux-命令行命令
    【面试】说出你的三个优点和三个缺点
    [ 隧道技术 ] 反弹shell的集中常见方式(三)powercat反弹shell
    Ajax——取消请求
    【ELM分类】基于matlab遗传算法优化ELM神经网络数据分类【含Matlab源码 2138期】
  • 原文地址:https://blog.csdn.net/m0_63144319/article/details/132722595