Issue
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); } }
Solution
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
Inside configure(HttpSecurity http), .antMatchers("/login","/","/project/jira/update","/leave",
"/v2/api-docs",
"/swagger-ui.html").permitAll();
Inside configure(WebSecurity web), web.ignoring().antMatchers("/swagger-resources/", "/webjars/") .antMatchers(HttpMethod.OPTIONS, "/**");
JwtTokenAUthentication filter class
- 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();
Answered By - Narayana Rao Kancharana
Answer Checked By - David Goodson (JavaFixing Volunteer)