java Swagger-ui一直显示示例宠物商店而不是我的API

bnl4lu3b  于 2023-04-04  发布在  Java
关注(0)|答案(2)|浏览(541)

我对此有点困惑,因为它通常是开箱即用的。我正在制作一个小型的java spring-boot rest API,为了获得一个漂亮的API desc和测试页面,我使用了Swagger。除了这次它不显示我的应用程序的东西,它显示默认的petstore页面。
这是我在我的pom中得到的(在我的其他应用程序中看起来也是一样的):

<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>

我还尝试了:

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

但没什么区别。我不知道接下来该怎么办。

6yt4nkrj

6yt4nkrj1#

你可能只是点击了错误的URL。这是我犯的一个错误。例如,在我的项目中:

  • http://localhost:8080/swagger-ui/index.html将向我展示PetSore API
  • http://localhost:8080/swagger-ui.html显示了我自己的API

OpenAPI page让我明白了这一点:

  • 然后,Swagger UI页面将在http://server:port/context-path/swagger-ui.html上可用,OpenAPI描述将在以下url上可用,用于json格式:http://server:port/context-path/v3/api-docs
  • server:服务器名称或IP
  • port:服务器端口
  • 上下文路径:应用程序的上下文路径
hfyxw5xn

hfyxw5xn2#

在我使用springboot 3的情况下,原因是我的安全过滤器不知道:http://127.0.0.7:9000/v3/api-docs/swagger-config
症状:我的swagger-ui/index.html显示我的宠物店时,我没有登录;当我登录的时候给我看了钱
解决方法:

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

相关问题