简体   繁体   English

Swagger REST API Spring 引导文档

[英]Swagger REST API documentation with Spring Boot

I want to use Swagger 2.0 with my Spring Boot RESTful web service to generate documentation.我想将 Swagger 2.0 与我的 Spring Boot RESTful web 服务一起使用来生成文档。 I have searched quite a bit for an answer to this.我已经搜索了相当多的答案。 Basically I have a Spring Boot project with a set of controllers and I want to document the API's.基本上我有一个带有一组控制器的 Spring Boot 项目,我想记录 API。 I have the following dependencies setup in my POM file.我的 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>

This is my Swagger configuration class with the @Configuration and @EnableSwagger2:这是我的 Swagger 配置 class 与 @Configuration 和 @EnableSwagger2:

      @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();
    }

}

From what I have gathered in reading a couple of other answers here that at this point I should be able to see something at a URL such as http://myapp/v2/api-docs or alternatively http://localhost:8080/myapp/api-docs I have made the assumption that the "myapp" portion of the above URL refers to the name of the class in which my main resides (is this correct)?根据我在这里阅读其他几个答案时收集到的信息,此时我应该能够在 URL 看到一些东西,例如http://myapp/v2/api-docs或者http://localhost:8080/ myapp/api-docs我假设上面 URL 的“myapp”部分指的是我的 main 所在的 class 的名称(这是正确的)吗? Also I have tried this with port 8080 and port 80 and the bottom line is that I see nothing other than site can't be reached.我也用端口 8080 和端口 80 尝试过这个,最重要的是我除了无法访问站点之外什么也看不到。 I have looked at the answers provided here and here however I'm not having any success.我查看了此处此处提供的答案,但没有取得任何成功。 Any help would be much appreciated, thank you in advance.任何帮助将不胜感激,在此先感谢您。

As you can see on the following documentation : https://springfox.github.io/springfox/docs/snapshot/#springfox-swagger-ui 正如您在以下文档中看到的那样: https : //springfox.github.io/springfox/docs/snapshot/#springfox-swagger-ui

The endpoint is now on swagger-ui.html, for your case, it will be http://localhost:8080/myapp/swagger-ui.html 端点现在在swagger-ui.html上,对于您而言,它将是http:// localhost:8080 / myapp / swagger-ui.html

I used, <artifactId>springdoc-openapi-ui</artifactId> with我用<artifactId>springdoc-openapi-ui</artifactId>

public class OpenApiConfiguration{

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM