How to display app API documentation by using spri

2020-06-17 02:01发布

问题:

I read this https://springdoc.github.io/springdoc-openapi-demos/ documentation to use springdoc-openapi-webflux-ui. As documentation said I just added springdoc-openapi-webflux-ui library to my app: implementation('org.springdoc:springdoc-openapi-webflux-ui:1.2.26')

Additionally, I customized path to API in application.yml:

springdoc:
  swagger-ui:
    path: /swagger-ui.html

When I start app, and go to http://localhost:8080/swagger-ui.html, it redirects me to http://localhost:8080/webjars/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config. In that page, I got an error:

Whitelabel Error Page
This application has no configured error view, so you are seeing this as a fallback.
Mon Jan 20 05:16:10 UTC 2020
[7192d9dc] There was an unexpected error (type=Not Found, status=404).
No matching handler

Question is: Should I add additional configurations to my app to show API documentation?

PS: I use spring.boot 2.2.2:RELEASE

回答1:

By default, you just need to add the dependency of the springdoc-openapi-webflux-ui.

 <dependency>
      <groupId>org.springdoc</groupId>
      <artifactId>springdoc-openapi-webflux-ui</artifactId>
      <version>1.2.32</version>
   </dependency>

You can have a look at the demos code:

  • https://github.com/springdoc/springdoc-openapi-demos.git-

You can check you classpath, and try to run the application from outside the IDE. Make sure you have the correct settings of your IDE depending on your build Tools:

  • https://github.com/springdoc/springdoc-openapi/issues/361

Also, please check if you are using @EnableWebFlux.

As stated in the Spring Boot reference documentation, when you use @EnableWebFlux, you tell Spring Boot that you wish to take full control over the WebFlux configuration and disable all auto-configuration for this (including static resources):

  • https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-webflux-auto-configuration

You have two solutions:

  1. Remove @EnableWebFlux
  2. If you really want take control of Bean creation and you want absolutely to use @EnableWebFlux, then you need to add an implementation of WebFluxConfigurer.

This has been discussed here:

  • https://github.com/springdoc/springdoc-openapi/issues/402