Swagger-ui keeps showing example petstore instead of my API
Asked Answered
T

4

6

I'm a bit lost about this, because it usually works out of the box. I'm making a small java spring-boot rest api, and to get a nice API desc and test page, I use Swagger. Except this time it doesn't show my app's stuff, it shows the default petstore page.

This is what my I got in my pom (and it looks the same in other apps of mine):

    <properties>
        <java.version>11</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-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.14.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.14.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.14.0</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.7.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.7.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-ui</artifactId>
            <version>1.5.6</version>
        </dependency>
        <!-- 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>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.19</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

I also tried with:

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>${springfox.version}</version>
        </dependency>

But it didn't make a difference. Not sure where to go from here.

Tullius answered 30/3, 2021 at 8:40 Comment(0)
H
8

You might just be hitting the wrong URL. That was a mistake I was making. In my project for example:

  • http://localhost:8080/swagger-ui/index.html will show me the PetSore API
  • http://localhost:8080/swagger-ui.html shows me my own API

The OpenAPI page clued me in on this:

  • 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
Husch answered 22/3, 2022 at 14:2 Comment(0)
U
5

In my case with springboot 3, the reason was my security filter don't known about: http://127.0.0.7:9000/v3/api-docs/swagger-config

symptom: My swagger-ui/index.html showed me the Petshop when I was not login; and showed me the money when I did login;

workaround:

@EnableWebSecurity
@Configuration(proxyBeanMethods = false)
public class DefaultSecurityConfig {

    @Bean
    SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception {
        http
            .authorizeHttpRequests(authorize ->
                authorize.requestMatchers(
                "/swagger-ui.html","/swagger-ui/**",
                "/v3/api-docs/**" //<<---(1)
                ).permitAll())
            .authorizeHttpRequests(authorize ->
                authorize.anyRequest().authenticated()

        )
            .formLogin(withDefaults());
        return http.build();
    }
...
Urinate answered 3/4, 2023 at 10:18 Comment(0)
D
3

In my case, the two URLs (swagger-ui/index.html and swagger-ui.html) were equivalent because the first one redirects to the second one. The issue was much simpler. In Swagger UI itself, there is a textbox for the opendoc source with the default value for the Petstore API. To load your own API simply replace the URL in the textbox with:

{Application-Context}/v3/api-docs

and hit explore.

Dereliction answered 23/3 at 0:1 Comment(0)
E
0

just clear chrome cache after changing swagger config

Eleemosynary answered 26/9 at 6:7 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Imperceptive

© 2022 - 2024 — McMap. All rights reserved.