Home > database >  Swagger-ui.html is giving blank page
Swagger-ui.html is giving blank page

Time:12-13

When I am trying to enable swagger in my spring boot application, it is showing blank page. I am using url https://localhost:8080/context-path/swagger-ui/ I am able to get the JSON data for url https://localhost:8080/context-path/v2/api/docs I am not getting where this going wrong for swagger ui can anyone help me on this. Thanks in advance.

Swagger config file

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

I have added dependencies in 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>

I have controller like this

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

CodePudding user response:

springfox-boot-starter ships with swagger-ui and swagger-2 Reference

Could you please try removing the explicitly added dependencies and access /swagger-ui/ or /swagger-ui/index.html

CodePudding user response:

Have you tried using the same Springfox version in all dependencies as follows?

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

Nevertheless and I know this does not solve your problem directly, but I would suggest you to consider moving to springdoc. Springfox is so buggy at this point that is a pain to use. I've moved to springdoc 2 years ago because of its Spring WebFlux support and I am very happy about it. Additionally, it also supports Kotlin Coroutines, which I am not sure Springfox does.

If you decide to migrate, springdoc even has a migration guide.

CodePudding user response:

After a lot of time it is got worked for me,In my application I am using spring security so configurations were mismatched. That's why I got blank page.

I did following changes in Security config and JwtTokenAUthenticationFilter classes

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

  2. "/v2/api-docs",

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

  3. Inside configure(WebSecurity web), web.ignoring().antMatchers("/swagger-resources/", "/webjars/") .antMatchers(HttpMethod.OPTIONS, "/**");

JwtTokenAUthentication filter class

  1. Inside JwtTokenAuthenticationFilter's doFilterInternal -> uri.indexOf("swagger") >= 0 || "/{context-path}/v2/api-docs".equals(uri)

or uri.contains("swagger")||uric.contains("/v2/api/docs")

here uri means HttpServletRequest.getRequestURI();

  • Related