【问题标题】:Swagger-ui.html is giving blank pageSwagger-ui.html 给出空白页
【发布时间】:2022-01-16 02:06:14
【问题描述】:

当我尝试在我的 Spring Boot 应用程序中启用 swagger 时,它显示空白页。 我正在使用网址 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); } }

【问题讨论】:

  • 您是否定义了任何控制器?可以分享一下吗?
  • 你能把这个添加到问题中吗?在评论中几乎不可能读到这一点。

标签: java spring-boot swagger-ui springfox


【解决方案1】:

springfox-boot-starter 附带 swagger-ui 和 swagger-2 Reference

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

【讨论】:

    【解决方案2】:

    您是否尝试过在所有依赖项中使用相同的 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 在这一点上是如此的错误,以至于使用起来很痛苦。由于 Spring WebFlux 支持,我在 2 年前搬到了 springdoc,对此我感到非常高兴。此外,它还支持 Kotlin Coroutines,我不确定 Springfox 是否支持。

    如果您决定迁移,springdoc 甚至还有 migration guide

    【讨论】:

    • 我曾尝试使用上述版本,但没有运气。好的,我试试谢谢
    【解决方案3】:

    经过很长时间,它对我有用,在我的应用程序中,我使用的是 Spring Security,因此配置不匹配。这就是我得到空白页的原因。

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

    1. 内部configure(HttpSecurity http), .antMatchers("/login","/","/project/jira/update","/leave",

    2. "/v2/api-docs",

      "/swagger-ui.html").permitAll();

    3. 内部配置(WebSecurity web), web.ignoring().antMatchers("/swagger-resources/", "/webjars/") .antMatchers(HttpMethod.OPTIONS, "/**");

    JwtTokenAUthentication 过滤器类

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-28
      • 2018-06-08
      • 2011-10-20
      • 1970-01-01
      • 2017-02-19
      • 1970-01-01
      • 1970-01-01
      • 2020-10-20
      相关资源
      最近更新 更多