Failed to load API definition in Springboot
Asked Answered
H

11

7

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.

http://localhost:8080/v3/api-docs

Haught answered 24/3, 2020 at 17:23 Comment(1)
Maybe Swagger UI default URL is localhost:8080/swagger-ui.html ? Just try.Drove
C
6

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:

enter image description here

After I add the path /v3/api-docs into the page text field and click on explore button the documentation shown.

enter image description here

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.

Cordelier answered 16/7, 2020 at 17:23 Comment(0)
D
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();

Decent answered 21/2, 2022 at 19:14 Comment(1)
authorizeRequests is deprecated in spring boot 3Clout
N
4

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));
}
Newmown answered 10/9, 2021 at 0:4 Comment(1)
this solution fixed my problem by adding the code in class WebSecurityConfig extends WebSecurityConfigurerAdapterHeaded
H
2

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=

Haught answered 25/3, 2020 at 18:18 Comment(0)
R
2

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/**");
    }
}
Rogozen answered 18/2, 2022 at 0:37 Comment(0)
F
0

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>
Finally answered 4/1, 2022 at 8:31 Comment(0)
S
0

Use

http.anyRequest().permitAll(); 

Can be treat as temporary solution

Sexpot answered 11/6, 2022 at 19:17 Comment(2)
This is not a solution to the problem. You are allowing all requests to your serviceTerraqueous
That's why mentioned as a temporary solution.Sexpot
S
0

if you are using spring security then add below config path security config file

http.authorizeRequests().antMatchers("/v3/api-docs/**","/swagger-ui/**").permitAll()
Sexpot answered 29/11, 2022 at 5:42 Comment(0)
Q
0

in springdoc.api-docs.path=/api-app in your maven project try to change the path of swagger and try again i hope it will works

Quadroon answered 19/6, 2023 at 5:21 Comment(0)
P
0

I solved by adding :

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webmvc-api</artifactId>
    <version>2.2.0</version>
</dependency>
<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
    <version>2.2.0</version>
</dependency>
Piggyback answered 6/7, 2024 at 14:28 Comment(0)
S
0

From what I see, it is usually due to not add open security in WebSecurityConfig for SpringBoot 3.0.0+

@Configuration
@EnableWebSecurity
public class WebSecurityConfig {

    private static final String[] EXCLUDED_PATTERNS = {
        "/**/actuator/**",
        "/**/v3/api-docs/**",
        "/**/configuration/ui/**",
        "/**/swagger-resources/**",
        "/**/configuration/**",
        "/**/swagger-ui.html",
        "/**/swagger-ui/**",
        "/**/webjars/**"
    };

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.authorizeHttpRequests((requests) -> requests
            .requestMatchers(EXCLUDED_PATTERNS).permitAll()
            .anyRequest().authenticated()
        ).csrf(AbstractHttpConfigurer::disable);
    }
}
Semiconscious answered 6/8, 2024 at 4:44 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.