chris
chris

Reputation: 419

Swagger-ui keeps showing example petstore instead of my API

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.

Upvotes: 8

Views: 12396

Answers (4)

rainywindow
rainywindow

Reputation: 61

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();
    }
...

or

if you are using Spring Security

http
   .authorizeHttpRequests(auth -> auth
    .requestMatchers("/swagger-ui/**","/v3/api-docs/**").permitAll()
      .anyRequest().authenticated())
   


Upvotes: 6

Freeman
Freeman

Reputation: 1

just clear chrome cache after changing swagger config

Upvotes: 0

AHH
AHH

Reputation: 1083

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.

Upvotes: 4

Chris
Chris

Reputation: 396

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

Upvotes: 8

Related Questions