找不到swagger-ui页面

xxls0lw8  于 8个月前  发布在  其他
关注(0)|答案(2)|浏览(178)

我对swagger的用户界面有问题。我正在使用这些依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>jakarta.xml.bind</groupId>
        <artifactId>jakarta.xml.bind-api</artifactId>
    </dependency>

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>

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

    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt</artifactId>
        <version>0.9.1</version>
    </dependency>

    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-starter-webmvc-api</artifactId>
        <version>2.2.0</version>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>

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

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

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
    </dependency>

</dependencies>

所以,正如你所看到的,对于swagger,我使用了这个依赖项:

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webmvc-api</artifactId>
    <version>2.2.0</version>
</dependency>

这些是我在application.properties中的swagger设置

# Swagger settings
springdoc.swagger-ui.enabled=true
springdoc.api-docs.path=/api-docs
springdoc.swagger-ui.path=/swagger-ui.html

这是我的SecurityFilterChain。我以为SpringSecurity可能会阻止我的请求,但看起来并没有

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    http.cors().and().csrf().disable()
            .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
            .authorizeHttpRequests((requests) -> requests
                    .requestMatchers("/api/auth/**").permitAll()
                    .requestMatchers("/swagger-ui.html").permitAll()
                    .requestMatchers("/api-docs/**").permitAll()
                    .requestMatchers("/api/users/**").hasRole("USER")
                    .requestMatchers("/api/admin/**").hasRole("ADMIN")
                    .anyRequest().permitAll()
            );

    http.authenticationProvider(authenticationProvider());

    http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);

    return http.build();
}

我试图找到swagger ui在这个网址

http://localhost:8080/swagger-ui.html

我得到了白标签错误页面。Api-docs页面

http://localhost:8080/api-docs

工作得很好,那么问题在哪里呢?

vuktfyat

vuktfyat1#

问题出在错误的依赖。我是用这个

<dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-starter-webmvc-api</artifactId>
        <version>2.2.0</version>
 </dependency>

但看起来这个依赖只用于/api-docs路径,没有ui,所以我检查了我的旧项目并从那里复制了依赖

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

现在一切正常始终需要检查是否使用了正确的依赖项。

qnakjoqk

qnakjoqk2#

通常,如果你访问/swagger-ui.html,你会有一个重定向到/swagger-ui/index.html,只需更改路径:

.requestMatchers("/swagger-ui/**").permitAll()

然后尝试打开swagger url:
http://localhost:8080/swagger-ui/index.html

更新

声明OpenApi的自定义配置

@Configuration
public class ApiConfig {

  @Bean
  public OpenAPI customOpenApi() {
    return new OpenAPI().info(new Info().title("Application API")
            .version("version")
            .description("description")
            .license(new License().name("Apache 2.0")
                .url("http://springdoc.org"))
            .contact(new Contact().name("username")
                .email("[email protected]")));
  }
}

相关问题