69

I'm trying to add swagger-ui (OpenAPI 3.0) to a Spring Boot v3 application.

I've added the openapi-ui maven dependency, and it should work as per the documentation.

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

But apparently, it still doesn't work and localhost:8080/swagger-ui.html returns an 404 error.

What am I missing?

enter image description here

Brian Clozel
  • 56,583
  • 15
  • 167
  • 176
Nadar
  • 1,521
  • 2
  • 11
  • 21
  • If the solutions below does not solve the problem, you might check my answer on **[How to run Swagger 3 on Spring Boot 3](https://stackoverflow.com/questions/74614369/how-to-run-swagger-3-on-spring-boot-3)** – Murat Yıldız Mar 18 '23 at 09:01

12 Answers12

104

According to the documentation:

For spring-boot 3 support, make sure you use springdoc-openapi v2

https://springdoc.org/v2/

For the integration between spring-boot and swagger-ui, add the library to the list of your project dependencies (No additional configuration is needed)

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
    <version>2.0.0</version>
</dependency>

This will automatically deploy swagger-ui to a spring-boot application:

Documentation will be available in HTML format, using the official swagger-ui jars

The Swagger UI page will then be available at http://server:port/context-path/swagger-ui.html and the OpenAPI description will be available at the following url for json format: http://server:port/context-path/v3/api-docs

server: The server name or IP

port: The server port

context-path: The context path of the application

Documentation can be available in yaml format as well, on the following path : /v3/api-docs.yaml

Please note that modules have been renamed:

https://springdoc.org/v2/#migrating-from-springdoc-v1

enter image description here

nakhodkin
  • 1,327
  • 1
  • 17
  • 27
JCompetence
  • 6,997
  • 3
  • 19
  • 26
  • 4
    This kind of weird. Accoding to documentation https://springdoc.org/ in intoduction section it states. Library supports OpenAPI 3, Spring-boot (v1, v2 and v3) which is apparently not truth. In spring boot 3 environment something changes in spring factories and springdoc beans are not instantiated at all. – simar Feb 21 '23 at 12:19
  • 2
    Still not working for me even with Spring Boot 3 and the right dependencies. – Monkey Supersonic Mar 09 '23 at 17:11
  • 1
    Make sure you replace the springdoc-openapi-ui dependency by the springdoc-openapi-starter-webmvc-ui one. If you use both of them the application will not start – Hugo Vinhal Mar 18 '23 at 23:39
19

I completely agreed with @JCompetence. Please note that springdoc-openapi-ui now changed to springdoc-openapi-starter-webmvc-ui from spring boot 3.

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

Please try this. If you want to find more info, please check the official link: https://springdoc.org/v2/#features

nakhodkin
  • 1,327
  • 1
  • 17
  • 27
Suman Maity
  • 261
  • 1
  • 3
11

For me it helped, just changed the dependency

   implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.11'

to

    implementation group: 'org.springdoc', name: 'springdoc-openapi-starter-webmvc-ui', version: '2.0.0'

or

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
    <version>2.0.0</version>
</dependency>
nakhodkin
  • 1,327
  • 1
  • 17
  • 27
9

You need to use springdoc-openapi

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
    <version>2.0.4</version>
</dependency>

for kotlin you must add one dependency more

<dependency>
    <groupId>com.fasterxml.jackson.module</groupId>
    <artifactId>jackson-module-kotlin</artifactId>
</dependency>

then add `@OpenAPIDefinition on your main class

@SpringBootApplication
@OpenAPIDefinition
class MyApplication {
}
Arif iskandar
  • 91
  • 1
  • 4
6

springdoc-openapi-starter-webmvc-ui doesn't work with spring-boot-starter-webflux until you include spring-boot-starter-web. If you include spring security then it is dead.

helvete
  • 2,455
  • 13
  • 33
  • 37
2

Just an add-on if your application have spring security enabled !! Then you will need to whitelist swagger-endpoint to not use authentication

@Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.csrf().disable().authorizeHttpRequests(auth -> {
            auth.requestMatchers("/v3/**", "/swagger-ui/**").permitAll();
            auth.anyRequest().authenticated();
        });

return httpSecurity.build();
Jay Yadav
  • 236
  • 1
  • 2
  • 10
1

Maven plugin to generate (Java) code from OpenApi spec (.yml files)

Generator “spring” supports Jakarta namespace

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <configuration>
        <configOptions>
            ...
            **<useSpringBoot3>true</useSpringBoot3>**
        </configOptions>
    </configuration>
</plugin>

Generator “java” (to generate a client) doesn’t support Jakarta namespace yet. So use the Eclipse Transformer plugin (Javax dependencies need provided scope!)

 <plugin>
        <groupId>org.eclipse.transformer</groupId>
        <artifactId>transformer-maven-plugin</artifactId>
        <extensions>true</extensions>
        <configuration>
          <rules>
           <jakartaDefaults>true</jakartaDefaults>
         </rules>
       </configuration>
        <executions>
            <execution>
                <id>jakarta-ee</id>
                <goals><goal>jar</goal></goals>
                <phase>package</phase>
                <configuration>
                    <artifact>
                        <groupId>${project.groupId}</groupId>
                        <artifactId>${project.artifactId}</artifactId>
                    </artifact>
                </configuration>
            </execution>
        </executions>
    </plugin>
DV Singh
  • 1,038
  • 11
  • 16
1

You also need to add this annotation to your SpringBootApp main class: @OpenAPIDefinition

@SpringBootApplication
@OpenAPIDefinition
class MyApplication
1

For Spring Security support use the Below dependency

implementation("org.springdoc:springdoc-openapi-starter-common:2.1.0")
implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.1.0")

from https://springdoc.org/v2/#migrating-from-springfox

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
1

I got the same error and solved it by changing the following dependency

from : implementation 'org.springdoc:springdoc-openapi-ui:1.6.15'

to : implementation group: 'org.springdoc', name: 'springdoc-openapi-starter-webmvc-ui', version: '2.0.0'

L_Cleo
  • 1,073
  • 1
  • 10
  • 26
0

Also please have in mind this compatibility matrix for spring boot:

Compatibility matrix

I would also advice that you delete the cookies of your browser for the host you are trying to access swagger on!

a.valchev
  • 388
  • 4
  • 10
0

in my case, you have to add below lines in application.yml:
springdoc: api-docs: path: /api-docs swagger-ui: path: /swagger-ui.html enabled: true

Maxwell Cheng
  • 1,050
  • 10
  • 17