I am trying to integrate Swagger into my Spring Boot application. My spring boot version is 2.4.5. I have included the following dependencies in my pom.xml file:
<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>
This is my Swagger configuration class:
@Configuration
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
I've included annotation @EnableSwagger2 on my main class. This is my webSecurityClass:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, jsr250Enabled = true, prePostEnabled = true)
class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
JwtRequestFilter jwtRequestFilter;
@Autowired
private AppConfigProperties appConfigProperties;
@Bean
public AccessDeniedHandler accessDeniedHandler() {
return new CustomAccessDeniedHandler();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors();
http.authorizeRequests().antMatchers("/temp").permitAll()
.and().csrf().disable();
http.authorizeRequests()
.antMatchers("/webjars/**", "/swagger-ui/**", "/v3/api-docs", "/swagger-resources/**", "/v2/api-docs", "/v2/**", "/swagger-ui.html").permitAll()
.anyRequest().authenticated()
.and().httpBasic()
.and().csrf().disable();
http.addFilterAfter(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
if (SecurityContextHolder.getContext().getAuthentication() == null) {
System.out.println("WebSecurityConfig coming here ---------> ");
}
}
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedMethods("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS")
.allowedOrigins(appConfigProperties.getConfigValue("origin"));
}
};
}
}
If I access the URL http://localhost:8080/swagger-ui.html, I get the response:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Mon May 01 19:10:32 IST 2023
There was an unexpected error (type=Not Found, status=404).
If I access the URL http://localhost:8080/swagger-ui/index.html, I get the following response:
Unable to infer base URL. This is common when using dynamic servlet registration or when the API is behind an API Gateway. The base URL is the root of where all the Swagger resources are served. For example, if the API is available at http://example.org/api/v2/api-docs, then the base URL is http://example.org/api/. Please enter the location manually:
What is wrong with my configuration? I tried many things but it is not working. If I call http://localhost:8080/v3/api-docs through Postman, it's working.
springfox
does not work properly when used with spring-boot version greater than 2.4. I faced the same error and fixed it by using springdocs
instead of springfox
.
You can remove springfox
dependency from pom.xml
and use below instead-
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.7.0</version>
</dependency>
You can remove @EnableSwagger2
annotation as well.
The API docs can be accessed on http://localhost:8080/swagger-ui/index.html