Suddenly Springfox Swagger 3.0 is not working with spring webflux
Asked Answered
M

8

29

Application was working with Springfox Swagger 3.0 few days back. Suddenly it is stopped working. The Jar file which was created before a week is still working but now when we try to build a new Jar file, which is not working, even without any code/library changes. I have even referred the below URL but still facing issue.

404 error with swagger-ui and spring webflux

Below given my configuration:

POM file:

<properties>
    <java.version>1.8</java.version>
    <springfox.version>3.0.0-SNAPSHOT</springfox.version>
    <spring.version>2.3.1.RELEASE</spring.version>
</properties>
<repositories>
    <repository>
        <id>spring-libs-milestone</id>
        <name>Spring Milestone Maven Repository</name>
        <url>http://oss.jfrog.org/artifactory/oss-snapshot-local/</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>${springfox.version}</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-spring-webflux</artifactId>
        <version>${springfox.version}</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>${springfox.version}</version>
    </dependency>
</dependencies>

Config Files:

@Configuration
@EnableSwagger2WebFlux
public class SwaggerConfiguration implements WebFluxConfigurer {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .description("My Reactive API")
                        .title("My Domain object API")
                        .version("1.0.0")
                        .build())
                .enable(true)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.reactive.controller"))
                .paths(PathSelectors.any())
                .build();

    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/swagger-ui.html**")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

I am getting 404 error when I try to open the swagger page.

http://localhost:8080/swagger-ui.html

Can someone help me with this. Thanks in advance.

Morganmorgana answered 7/7, 2020 at 10:28 Comment(3)
Code doesn’t magically change, some change must have been done, provide debug logs.Grocery
You use Snapshot version from Springfox. Snapshot dependencies are free to change anytime. Springfox team are working on creating a final version. Check out issues on Github for more details: github.com/springfox/springfox/issuesOnto
If you are using Spring Security, use the security configuration mentioned on Cannot open Swagger UI in tis Version 3 in my Spring Boot Example page.Totem
P
68

The implementation has changed recently (see migrating from earlier snapshots for a brief update on this).

Now the UI is avaiable under /swagger-ui endpoint (not /swagger-ui.html).

You should also drop the @EnableSwagger2WebFlux annotation and addResourceHandlers() method, remove all springfox dependencies and add just one:

<dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-boot-starter</artifactId>
        <version>${springfox.version}</version>
</dependency>
Paulo answered 8/7, 2020 at 8:2 Comment(7)
Is it possible to customize the swagger ui path.? Instead of localhost:8080/swagger-ui/ I want to use localhost:8080/mycontextpath/swagger-ui/. As I am using netty server, Didn't get the default context path option.Morganmorgana
try this in application.property file: springfox.documentation.swagger-ui.base-url=/mycontextpathLogway
@PraveenD Try to go to your applicaiton.properties. And there add server.servlet.context-path=mycontextpath. The spring take care of the contextpath.Sabbat
You may need to run mvn clean before the changes will take effect.Infinitesimal
BTW, the / at end of /swagger-ui/ is necessary.Fulminate
@jrd..Hello...What things to change if using non boot spring mvc?.. I am having the same issue with non bootable spring4 mvc project.Backbone
BTW, it may be better to use /swagger-ui/index.html instead of /swagger-ui/, to avoid some character encoding issues.Urogenous
B
20

Getting Started with Swagger-3 in Springboot Rest API

For new projects

For Maven:-

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

For Gradle:-

  implementation "io.springfox:springfox-boot-starter:<version>"

Now there is no extra configuration to activate swagger on the spring-boot project like the previous. if try to configure with security, there is some configuration. plz refer to this article.

In swagger version 3 remove the @EnableSwagger2 annotation base config also.

And most of the user tries to find HTML swagger document file using {host}/swagger-ui.html or {host}/swagger-ui those are now removed.

use {host}/swagger-ui/ to see the HTML document

This is a sample project link on GitHub Refer to documentation io.springfox

Bubonocele answered 25/10, 2020 at 18:43 Comment(0)
L
6

This is how it worked for me. I am using InteliJ IDEA, SpringBoot and Maven.

When adding Swagger dependencies as:

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

They were colored in red and I could not add them at all. I tried reloading my project, generate sources and update folders, but I just couldn't install it.

Then I added this dependency as well:

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

And it worked just fine. Also, I find my conclusion to the problem here:

Baeldung

So in the end I had this in my pom.xml file:

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

To see Swagger UI in your browser using Swagger 3.0 is

http://localhost:8080/swagger-ui/index.html

Hope it helps to someone :)

Linnealinnean answered 10/3, 2021 at 17:32 Comment(1)
@vuhoanghiep1993 I am glad :)Linnealinnean
B
4

Use only this dependecy without io.springfox dependencies

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

Now there is no extra configuration to activate swagger on the spring-boot project like the previous.

In swagger version 3.0.0 remove the @Configuration and @EnableSwagger2 and annotations.

use {host}/swagger-ui/ to see the HTML document, {host}/swagger-ui.html or {host}/swagger-ui those are now removed. Schneider

Behl answered 18/1, 2022 at 13:39 Comment(1)
I'm getting the following error, when trying to access localhost:8080/swagger-ui/: org.springframework.security.web.firewall.RequestRejectedException: The request was rejected because the URL contained a potentially malicious String "//"Boren
C
2

Ok, after reading everything and after trying most, got my swagger-ui on /swagger-ui/index.html#

https://github.com/springfox/springfox-demos/tree/master/boot-swagger

Crispate answered 21/5, 2021 at 10:9 Comment(0)
L
1

If you are working with latest version of spring boot application, follow the below steps to enable swagger in your application.

Step-1 : Add below dependency into pom.xml

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

Step-2 : Add this property into application.properties file.

spring.mvc.pathmatch.matching-strategy = ANT_PATH_MATCHER

Step-3 : Start the Spring-Boot application.

Strp-4 : Browse this url http://localhost:8080/swagger-ui/

Note: 1.

  • Apart from the above given maven dependency, not any other swagger dependency required.

  • if you were using older version of swagger and moving into newer version then flow the above steps and remove the below annotation from your application as this not required any more.

    @EnableSwagger2WebFlux

Lely answered 30/11, 2022 at 7:24 Comment(2)
If I'm working with gradle will this work spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER?Nansen
It should work, there is nothing related with gradle or maven.Lely
S
1

All you need to do is this:

This is no longer correct, all you need is this dependencies:

        <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
        <version>2.2.0</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-boot-starter</artifactId>
        <version>3.0.0</version>
    </dependency>

and than head to : http://localhost:8080/swagger-ui/index.html

Surplusage answered 17/9, 2023 at 13:27 Comment(0)
I
0

Just add the following dependency to your pom.xml:

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

This work in Spring boot 3.1.0

By default, the Swagger UI can be accessed at the following URL:

/webjars/swagger-ui/index.html
Iila answered 30/5, 2023 at 8:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.