Search code examples
javaspringspring-bootopenapispringdoc-openapi-ui

Springdoc with Boot 3, no explicit OpenAPI bean declaration


I have trouble creating Swagger/Open API endpoints for my app. There was a previous app where such an endpoint was created automatically. I want to avoid explicitly declaring an OpenAPI bean. My understanding was I only needed a springdic-openapi-ui dependency and a property for the swagger UI endpoint in case I want to override it (it defaults to /swagger-ui.html). However, in my recent app /swagger-ui.html that is not the case.

In the MRE below, GET /api/hello works as expected. GET /swagger-ui.html returns 404 which suggests the endpoint wasn't created by Swagger for some reason.

May be version compatibility issues: I now use Boot 3, the earlier app I mentioned used Boot 2.

Here's the MRE:

package com.example.openapi_demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class OpenapiDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(OpenapiDemoApplication.class, args);
    }

}
server.port=8080
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.3.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>openapi-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>openapi-demo</name>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
package com.example.openapi_demo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class DemoController {
    @GetMapping("/hello")
    public String getHello() {
        return "hello";
    }
}

Solution

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

    Use this one. Remove any other configuration you might have added. Access it on ${baseUrl}/swagger-ui/index.html