简体   繁体   中英

Can't get swagger2 to display swagger-ui.html

The problem

Am learning java spring boot and my problem is getting the swagger front-end to load from http://localhost:8080/swagger-ui.html#/ I get the console message as follows:

WARN 23432 --- [nio-8080-exec-9] osweb.servlet.PageNotFound: No mapping for GET /swagger-ui.html

Background

I've built out a starter project using spring boot with a basic API and have tested the endpoints with postman ok. I'm using v2.6.4 of spring-boot-starter-parent.

I'm trying out swagger for the first time and have included the following in my pom.xml

groupId io.springfox artifactId springfox-boot-starter version 3.0.0

In my application.yml I have added the following to resolve a build issue which was related to a version/dependency mismatch.

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher
  
  

I've added the following class to my config package based on a tutorial I am following.

@Configuration
@EnableWebMvc
@Import(SpringDataRestConfiguration.class)
public class ApplicationSwaggerConfig {

    @Bean
    public Docket speakersApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }

I found some articles saying to override resource handling as follows to cure the problem but it is not helping:

@Configuration
public class WebMvcConfigurer extends WebMvcConfigurationSupport {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
    registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
    registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    super.addResourceHandlers(registry);
}

In order to enable swagger ui using spring-fox, you need to add an additional dependency in your pom.

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>3.0.0</version>
</dependency>

(This is the latest version, you can use any version based on the usecase)

The path of the swagger UI will be at the below link or using swagger-ui.html page.

http://localhost:8080/your-app-root/swagger-ui/

I had a similar issue. The solution was to add a URL mapping for the controller. like this:

@RequestMapping("/myprefix")
@RestController
public class Controller {...}

Otherwise, it would resolve to the default one, which is "/". Somehow http://localhost:8080/your-app-root/swagger-ui/ is trying to be resolved to the controller which is intercepting all paths url starting with "/". If you change it to "/myprefix" as mentioned above, http://localhost:8080/your-app-root/swagger-ui/ would not be intercepted by this controller. As a dependency I used the following:

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

But the above solution would also work for

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency> 

Thats all you need to display swagger-ui.html.

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