简体   繁体   中英

How to display static swagger yaml file at application url /swagger

I created yaml file (openapi 3.0.0 format) as a documentation for our API. I would like to display this (static) swagger-ui yaml file at URL, where the application is running. Something like http://localhost:8080/swagger-ui . Where is displayed graphical representation of yaml file (same as here ). Yaml file is placed in root folder in project.

I am running application on java 11, springboot 2.1.5, building with maven.

I tried generate swagger yaml from code using

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

But this one is not perfect (missing default values, descriptions..)

I tried spring static resources with no success. The problem is that yaml file is not html.

Is there another (maybe better) way, how to display api documentation?

you need to add swagger UI dependency too.

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

then you can access swagger ui using,

http://localhost:9090/swagger-ui.html#/

try add this to your method,

public void yourMethod(@ApiParam(name = "id", required = true, value = "The id of the site", defaultValue = "3F7B07E2") String id)

We were able to finish this task using @Configugarion class

@Configuration
    public class SwaggerConfiguration implements WebMvcConfigurer {

      private final String swaggerUILocation = "whatEverLocationYouWant";
      private final String swaggerApiDocsLocation = "whatEverLocationYouWant";

      @Override
      public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(swaggerUILocation + "**")
            .addResourceLocations("classpath:/swagger-ui/");
        registry.addResourceHandler(swaggerApiDocsLocation + "**")
            .addResourceLocations("classpath:/swagger/");
      }
    }

Then we used swagger-ui jar file, unziped it into resources folder and replace one row in file index.html here:

<script>
      window.onload = function () {
        // Begin Swagger UI call region
        const ui = SwaggerUIBundle({
          url: "placeHereExactLinkToYourYamlFile",
          dom_id: '#swagger-ui',
          deepLinking: true,
          presets: [
            SwaggerUIBundle.presets.apis,
            SwaggerUIStandalonePreset
          ],
          plugins: [
            SwaggerUIBundle.plugins.DownloadUrl
          ],
          layout: "StandaloneLayout"
        })
        // End Swagger UI call region

        window.ui = ui
      }
    </script>

The swagger html is visible and is working next to an application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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