1

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] o.s.web.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);
}
mikepoole72
  • 65
  • 2
  • 9
  • This is sometimes weird, you could be trying every possible solution online. Keep changing or downgrading the version of the swagger dependency until it is fixed. This suggestion is not ideal but works for me all the time. – Young Emil Apr 15 '22 at 15:46

2 Answers2

0

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/
Parth Manaktala
  • 1,112
  • 9
  • 27
  • I did try and add that but I think it is already pulled in from this dependency that I have added. It didn't resolve the issue. My application root is directly off localhost btw, io.springfox springfox-boot-starter 3.0.0 – mikepoole72 Mar 15 '22 at 13:59
  • @mikepoole72 Did any of the answers in this thread help? https://stackoverflow.com/questions/43545540/swagger-ui-no-mapping-found-for-http-request – Parth Manaktala Mar 15 '22 at 14:04
  • swagger-ui location has moved from http://host/context-path/swagger-ui.html to http://host/context-path/swagger-ui/index.html OR http://host/context-path/swagger-ui/ for short. This makes it work much better with pulling it as a web jar and turning it off using configuration properties if not needed. [Changes in swagger-ui](http://springfox.github.io/springfox/docs/current/#getting-started) – Leo Lee Dec 15 '22 at 06:31
0

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.