Reputation: 121
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); } }
Upvotes: 6
Views: 20828
Reputation: 51
if you are using spring boot 3(3.1.2 in my case) with
implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0")
implementation("org.springframework.boot:spring-boot-starter-security")
please make sure permit openapi v3 public endpoint like this
new AntPathRequestMatcher("/v3/api-docs/**","GET"),
new AntPathRequestMatcher("/swagger-ui/**","GET"),
new AntPathRequestMatcher("/swagger-ui.html","GET")
and url below will work success
http://localhost:8080/swagger-ui.html
http://localhost:8080/v3/api-docs
Upvotes: 1
Reputation: 11
We migrate spring 3.1.0 to 3.1.2 and had a colateral effect,
This approach was working well in Spring boot 3.1.0
...
.requestMatchers("/v3/**","/swagger-ui/**").permitAll()
...
But in version 3.1.2 that approach got Blank screen on swagger uri /swagger-ui
...
.requestMatchers("/v3/**","/swagger-ui/**").permitAll()
...
To correct It, I used the constructor of AntPathRequestMatcher with a single parameter
...
.requestMatchers(new AntPathRequestMatcher("/v3/**")).permitAll()
.requestMatchers(new AntPathRequestMatcher("/swagger-ui/**")).permitAll()
...
Upvotes: 1
Reputation: 301
if you are extending 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);
}
In my case, my custom filter was causing problems, so you probably want just to review/refactor yours
Upvotes: 1
Reputation: 440
My solution was that request to swagger-ui was intercepted by a custom filter that was checking header for specific key,value.
Check your filters your filter may be intercepting your request and returning nothing leading to empty swagger ui page
Upvotes: 0
Reputation: 121
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
or uri.contains("swagger")||uric.contains("/v2/api/docs")
here uri means HttpServletRequest.getRequestURI();
Upvotes: 4
Reputation: 17510
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.
Upvotes: 1
Reputation: 395
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
Upvotes: 1