• Spring boot 集成Swagger


    Spring boot集成 Swagger找了很多资料,总结一下。

    前提是spring boot项目

    第一步导入jar包

            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.6.1</version>
            </dependency>
    
    
            <dependency>
                <groupId>io.swagger</groupId>
                <artifactId>swagger-annotations</artifactId>
                <version>1.5.13</version>
            </dependency>
    

      第二步 我们需要在代码中添加支持,于 Application 同级目录添加 Swagger 配置类.

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import springfox.documentation.builders.ApiInfoBuilder;
    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;
    
    @Configuration
    @EnableSwagger2
    public class Swagger2 {
    
        @Bean
        public Docket config() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .useDefaultResponseMessages(false)
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.pxx.xxx.controller"))
                    .build();
        }
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("Blog系统API文档")
                    .contact(new Contact("作者", "访问地址", "联系方式"))
                    .build();
        }
    }
    

      

    这时可以访问:

    http://localhost:8082/v2/api-docs测试返回数据

    第三步 使用Swagger-ui下载文件

    从官方 GitHub https://github.com/swagger-api/swagger-ui 下载整个项目包 、 解压, 然后运行 dist 目录中的文件 拷贝到项目resources下的public文件下,并且修改index.html

      const ui = SwaggerUIBundle({
    //http://localhost:8082/ 是项目的访问路径,注意更换
        url: "http://localhost:8082/v2/api-docs",
        dom_id: '#swagger-ui',
        deepLinking: true,
        presets: [
          SwaggerUIBundle.presets.apis,
          SwaggerUIStandalonePreset
        ],
        plugins: [
          SwaggerUIBundle.plugins.DownloadUrl
        ],
        layout: "StandaloneLayout"
      })
    

      

  • 相关阅读:
    安装composer后报错proc_open(): fork failed
    ZOJ4063 Tournament [The 2018 ACM-ICPC Asia Qingdao Regional Contest]
    BZOJ1191: [HNOI2006]超级英雄Hero
    BZOJ1270: [BeijingWc2008]雷涛的小猫
    BZOJ1303 [CQOI2009]中位数图
    BZOJ1192 [HNOI2006]鬼谷子的钱袋
    BZOJ1003 [ZJOI2006]物流运输 最短路+DP
    牛客国庆集训派对Day6 E-Growth
    BZOJ2208 [Jsoi2010]连通数
    BZOJ2761 [JLOI2011]不重复数字
  • 原文地址:https://www.cnblogs.com/nunuAction/p/7766596.html
Copyright © 2020-2023  润新知