'Failed to load API definition in Springboot
I am moving to Spring doc open Api and trying to hit the URL. I am getting the below error and logs from console.
URL : http://localhost:8080/swagger-ui/index.html?url=v3/api-docs
Logs :
2020-03-24 13:21:03.930 DEBUG 32622 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : GET "/swagger-ui/index.html?url=v3/api-docs", parameters={masked}
2020-03-24 13:21:03.931 DEBUG 32622 --- [nio-8080-exec-3] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler ["classpath:/META-INF/resources/webjars/"]
2020-03-24 13:21:03.933 DEBUG 32622 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : Completed 304 NOT_MODIFIED
2020-03-24 13:21:03.992 DEBUG 32622 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : GET "/swagger-ui/v3/api-docs", parameters={}
2020-03-24 13:21:03.993 DEBUG 32622 --- [nio-8080-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler ["classpath:/META-INF/resources/webjars/"]
2020-03-24 13:21:03.994 DEBUG 32622 --- [nio-8080-exec-4] o.s.w.s.r.ResourceHttpRequestHandler : Resource not found
2020-03-24 13:21:03.994 DEBUG 32622 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Completed 404 NOT_FOUND
2020-03-24 13:21:03.994 DEBUG 32622 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : "ERROR" dispatch for GET "/error", parameters={}
2020-03-24 13:21:03.995 TRACE 32622 --- [nio-8080-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : 2 matching mappings: [{ /error}, { /error, produces [text/html]}]
2020-03-24 13:21:03.995 TRACE 32622 --- [nio-8080-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest)
2020-03-24 13:21:03.996 DEBUG 32622 --- [nio-8080-exec-4] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Using 'application/json', given [application/json, */*] and supported [application/json, application/*+json, application/json, application/*+json]
2020-03-24 13:21:03.997 DEBUG 32622 --- [nio-8080-exec-4] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Writing [{timestamp=Tue Mar 24 13:21:03 EDT 2020, status=404, error=Not Found, message=No message available, (truncated)...]
2020-03-24 13:21:03.998 DEBUG 32622 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 404
I do see my JSON Response when i try to open the below URL.
Solution 1:[1]
I had this same issue today accessing a swagger URL like the following:
- http://localhost:6050/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config#/
The web browser showed the page:
After I add the path /v3/api-docs into the page text field and click on explore button the documentation shown.
Looking to the springdoc-openapi configuration I could see the default swagger ui path is /swagger-ui.html so when I acessed the following URL:
- http://localhost:6050/swagger-ui.html
It worked.
Solution 2:[2]
There could be the reason like when we start using spring security even then also we won't be able to access swagger-ui, here is what i tried and it started working.
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
.authorizeRequests().antMatchers("/user/register").permitAll().antMatchers("/swagger-ui/**").permitAll()
.antMatchers("/v3/api-docs/**").permitAll().anyRequest().authenticated();
http.apply(new JwtTokenConfigurer(tokenProvider));
}
Solution 3:[3]
Yes the URL to refer was incorrect. I tried with the new URL and it worked fine.
http://localhost:8080/swagger-ui/index.html?url=/v3/api-docs&validatorUrl=
Solution 4:[4]
If you are using spring-security you have to include the config path in your sercurity configuration. What I did in my security config was to exclude those paths from websecurity.
@Configuration
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/swagger-ui/**", "/v3/api-docs/**");
}
}
Solution 5:[5]
If you are using security, or something like security you should permit "../v3/api" files. I use like that
http.authorizeRequests().antMatchers("/swagger/**","/v3/**").permitAll();
Solution 6:[6]
In my case I had an outdate version of commons-lang3. Once I updated it to the version 3.10 it worked.
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Edu Costa |
| Solution 2 | Jeet Singh Parmar |
| Solution 3 | Tarun Pande |
| Solution 4 | Abderrahmane SARDAOUI |
| Solution 5 | Fatih Bayhan |
| Solution 6 | IKo |


