Tarun Pande
Tarun Pande

Reputation: 401

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.

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

Upvotes: 7

Views: 39673

Answers (11)

sendon1982
sendon1982

Reputation: 11234

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);
    }
}

Upvotes: 0

Abd Abughazaleh
Abd Abughazaleh

Reputation: 5515

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>

Upvotes: 0

Sk Faruk
Sk Faruk

Reputation: 1

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

Upvotes: 0

Srikant Upadhyay
Srikant Upadhyay

Reputation: 11

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

http.authorizeRequests().antMatchers("/v3/api-docs/**","/swagger-ui/**").permitAll()

Upvotes: 0

Srikant Upadhyay
Srikant Upadhyay

Reputation: 11

Use

http.anyRequest().permitAll(); 

Can be treat as temporary solution

Upvotes: 0

Fatih Bayhan
Fatih Bayhan

Reputation: 191

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();

Upvotes: 5

Abderrahmane SARDAOUI
Abderrahmane SARDAOUI

Reputation: 21

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/**");
    }
}

Upvotes: 2

IKo
IKo

Reputation: 5756

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>

Upvotes: 0

Jeet Singh Parmar
Jeet Singh Parmar

Reputation: 727

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));
}

Upvotes: 4

Edu Costa
Edu Costa

Reputation: 1460

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.

Upvotes: 6

Tarun Pande
Tarun Pande

Reputation: 401

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=

Upvotes: 2

Related Questions