【问题标题】:Swagger REST API documentation with Spring Boot使用 Spring Boot 的 Swagger REST API 文档
【发布时间】:2017-02-20 21:52:02
【问题描述】:

我想将 Swagger 2.0 与我的 Spring Boot RESTful Web 服务一起使用来生成文档。我已经搜索了相当多的答案。基本上我有一个带有一组控制器的 Spring Boot 项目,我想记录 API。我的 POM 文件中有以下依赖项设置。

<dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.4.0</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.5.0</version>
    </dependency>

这是我的带有@Configuration 和@EnableSwagger2 的Swagger 配置类:

      @Configuration
      @EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api(){
        return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.regex("/api/.*"))
            .build()
            .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            .title("My application title")
            .description("This is a test of documenting EST API's")
            .version("V1.2")
            .termsOfServiceUrl("http://terms-of-services.url")
            .license("LICENSE")
            .licenseUrl("http://url-to-license.com")
            .build();
    }

}

根据我在这里阅读其他几个答案时所收集到的信息,此时我应该能够在诸如 http://myapp/v2/api-docshttp://localhost:8080/myapp/api-docs 之类的 URL 上看到某些内容,我假设“myapp”上述 URL 的一部分是指我的主要所在的类的名称(这是否正确)?此外,我已经尝试使用端口 8080 和端口 80 进行此操作,最重要的是,除了无法访问站点外,我什么也看不到。我查看了herehere 提供的答案,但是我没有任何成功。任何帮助将不胜感激,在此先感谢您。

【问题讨论】:

标签: java spring rest swagger


【解决方案1】:

您可以在以下文档中看到: https://springfox.github.io/springfox/docs/snapshot/#springfox-swagger-ui

端点现在位于 swagger-ui.html 上,对于您的情况,它将是 http://localhost:8080/myapp/swagger-ui.html

【讨论】:

  • 谢谢,还有一个问题,URL 的“myapp”部分是我的“main”所在的类的名称吗?
  • 查看 netstat 时,我什至没有看到正在使用的端口 8080。
【解决方案2】:

我用过,&lt;artifactId&gt;springdoc-openapi-ui&lt;/artifactId&gt;

public class OpenApiConfiguration{

     @Bean
     public GroupedOpenApi abcApp(){
        String[] abcAppRootPath={"com.stockoverflow.swagger"};
        return GroupedOpenApi.builder().group("my app").packagesToScan(abcAppRootPath).build();
    }
}

参考:https://springdoc.org/#getting-started

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-21
    相关资源
    最近更新 更多