Spring Boot Swagger-ui.html提供空白页面

ua4mk5z4  于 2022-11-23  发布在  Spring
关注(0)|答案(4)|浏览(394)

当我试图在我的 Spring 启动应用程序中启用swagger时,它显示空白页面。我使用的是url https://localhost:8080/context-path/swagger-ui/我可以获得url 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); } }
h43kikqp

h43kikqp1#

经过大量的时间,它是得到了我的工作,在我的应用程序中,我使用的是Spring安全,所以配置不匹配。这就是为什么我得到了空白页。
我在安全配置和JwtTokenAUthenticationFilter类中进行了以下更改
1.内部配置(HttpSecurity http),.antMatchers(“/登录”,"/","/项目/jira/更新”,"/离开”,
1.“/v2/api-docs”,
“/swagger-ui.html”)。允许全部();
1.在配置(WebSecurity web)中,web.忽略().antMatchers(“/swagger-resources/",“/webjars/”).antMatchers(HttpMethod.OPTIONS,“/**”);
JwtToken身份验证筛选器类
1.在JwtTokenAuthenticationFilter的doFilterInternal内部-〉URI.索引的(“swagger”)〉= 0||“/{上下文路径}/v2/api-docs”.equals(uri)
或uri.包含(“swagger”)||尿酸。包含(“/v2/api/docs”)
这里uri表示HttpServletRequest.getRequestURI();

laawzig2

laawzig22#

您是否尝试在所有依赖项中使用相同的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在这一点上是如此的bug,这是一个痛苦的使用。我已经转移到springdoc 2年前,因为它的Spring WebFlux支持,我很高兴它。此外,它也支持Kotlin协程,我不确定Springfox是否这样做。
如果您决定进行迁移,springdoc甚至提供了迁移指南。

qpgpyjmq

qpgpyjmq3#

springfox-boot-starter随附swagger-ui和swagger-2 Reference
请尝试移除显式添加的依赖项并访问**/swagger-ui/**或/swagger-ui/index.html

nfzehxib

nfzehxib4#

我的解决方案是,swagger-ui的请求被一个自定义过滤器拦截,该过滤器正在检查特定键、值的标头。
检查您的过滤器您的过滤器可能会拦截您的请求,并返回什么导致空swagger ui页

相关问题