Swagger-ui.html显示空白页面

4

当我尝试在我的Spring Boot应用程序中启用Swagger时,它显示空白页面。 我正在使用URL https://localhost:8080/context-path/swagger-ui/ 我能够获取https://localhost:8080/context-path/v2/api/docs的JSON数据 我不知道这里出了什么问题,Swagger UI无法显示,有人可以帮忙吗?先感谢您。

Swagger配置文件

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
  
    @Bean
      public Docket api() { 
          return new Docket(DocumentationType.SWAGGER_2)  
            .select()                                  
            .apis(RequestHandlerSelectors.any())              
            .paths(PathSelectors.any())                          
            .build();                                           
      }
  
}

我已经在pom.xml中添加了依赖。
<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.5.1 </version>
  <relativePath /> <!-- lookup parent from repository -->
</parent>


<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger2</artifactId>
  <version>3.0.0</version>
</dependency>

<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-boot-starter</artifactId>
  <version>3.0.0</version>
</dependency>

<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger-ui</artifactId>
  <version>2.9.2</version>
</dependency>

我有这样的控制器
@GetMapping public ResponseEntity<ProjectDto> getAllProjects() { ProjectDto projectDto = new ProjectDto(); try { List<ProjectDto> projectDtos = projectService.getAllProjects(); projectDto.setProjectDtos(projectDtos.stream().collect(Collectors.toSet())); return new ResponseEntity<>(projectDto, HttpStatus.OK); } catch (ProjectNotFoundException exception) { LOGGER.info(exception.getMessage(), exception); projectDto.setErrorMsg(exception.getMessage()); return new ResponseEntity<>(projectDto, HttpStatus.BAD_REQUEST); } catch (Exception exception) { LOGGER.info(exception.getMessage(), exception); projectDto.setErrorMsg("Something went wrong with projects"); return new ResponseEntity<>(projectDto, HttpStatus.INTERNAL_SERVER_ERROR); } }

你有定义任何控制器吗?能分享一下吗? - whiplash
你能把那个加到问题里吗?在评论中读这个几乎是不可能的。 - whiplash
7个回答

3

经过很长一段时间的努力,我终于解决了问题。我的应用程序使用了Spring Security,因此配置不匹配导致了空白页面。

我在安全配置和JwtTokenAUthenticationFilter类中做了以下更改:

  1. 在configure(HttpSecurity http)中,添加了.antMatchers("/login",“/”,“/project/jira/update”、“/leave”,“/v2/api-docs”,“/swagger-ui.html”)。permitAll();

  2. 在configure(WebSecurity web)中,添加了web.ignoring().antMatchers("/swagger-resources/","/webjars/").antMatchers(HttpMethod.OPTIONS,“/**”);

JwtTokenAUthentication filter类

  1. 在JwtTokenAuthenticationFilter的doFilterInternal方法中,添加了uri.indexOf("swagger") >= 0 || "/{context-path}/v2/api-docs".equals(uri)。

或者 uri.contains("swagger")||uric.contains("/v2/api/docs")。

这里的uri指的是HttpServletRequest.getRequestURI()。


如果您正在扩展WebSecurityConfigurerAdapter - Kampaii

1
你尝试过在所有依赖项中使用相同的Springfox版本吗?如下所示:
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>3.0.0</version>
</dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>3.0.0</version>
</dependency>

尽管如此,我知道这并不能直接解决你的问题,但我建议你考虑迁移到springdoc。Springfox目前非常有缺陷,使用起来很痛苦。我在两年前就转向了springdoc,因为它支持Spring WebFlux,并且我对此感到非常满意。此外,它还支持Kotlin协程,我不确定Springfox是否支持。
如果你决定迁移,springdoc甚至还有一个迁移指南

我已经尝试使用以上版本,但没有成功。好的,我会尝试那个。谢谢。 - Narayana Rao Kancharana

0

springfox-boot-starter附带swagger-ui和swagger-2 参考

您能否尝试删除显式添加的依赖项并访问/swagger-ui/或/swagger-ui/index.html


0
我的解决方法是拦截swagger-ui的请求,使用自定义过滤器检查特定键值对的头部。
检查你的过滤器,因为它可能会拦截你的请求并返回空的swagger ui页面。

0
如果你正在扩展WebSecurityConfigurerAdapter
public static final String[] SWAGGER_AUTH_WHITELIST = new String[]{"/**/swagger-ui.html", "/**/webjars/**", "/**/swagger-resources", "/**/swagger-resources/**", "/**/v2/api-docs", "/**/configuration/ui", "/**/configuration/security", "/**/swagger-ui/**", "/**/v3/api-docs/**"};
// this one ignores under spring security
@Override
protected void configure(HttpSecurity http) throws Exception {
...
.antMatchers(SWAGGER_AUTH_WHITELIST).permitAll()
...
}
// this one ignores upon spring security(in my opinion, not the best option)
@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers(SWAGGER_AUTH_WHITELIST);
    super.configure(web);
}

在我的情况下,我的自定义过滤器出现了问题,所以你可能只需要审查/重构你的过滤器。

0
我们将Spring 3.1.0迁移到3.1.2,并产生了一个副作用,
这种方法在Spring Boot 3.1.0中运行良好。
...
.requestMatchers("/v3/**","/swagger-ui/**").permitAll()
...

但在3.1.2版本中,这种方法在swagger uri /swagger-ui上出现了空白屏幕。
...
.requestMatchers("/v3/**","/swagger-ui/**").permitAll()
...

为了修正它,我使用了AntPathRequestMatcher的构造函数,并传入了一个参数。
...
.requestMatchers(new AntPathRequestMatcher("/v3/**")).permitAll()
.requestMatchers(new AntPathRequestMatcher("/swagger-ui/**")).permitAll()
...

0
如果你正在使用Spring Boot 3(在我的情况下是3.1.2版本),那么
implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0")
implementation("org.springframework.boot:spring-boot-starter-security")

请确保允许打开类似这样的 OpenAPI V3 公共端点。
new AntPathRequestMatcher("/v3/api-docs/**","GET"),
new AntPathRequestMatcher("/swagger-ui/**","GET"),
new AntPathRequestMatcher("/swagger-ui.html","GET")

以下的链接将成功工作
http://localhost:8080/swagger-ui.html
http://localhost:8080/v3/api-docs

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接