Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

404 not found on swagger UI #361

Closed
ghost opened this issue Jan 21, 2020 · 35 comments
Closed

404 not found on swagger UI #361

ghost opened this issue Jan 21, 2020 · 35 comments
Labels
question Further information is requested

Comments

@ghost
Copy link

ghost commented Jan 21, 2020

Running Spring Boot 2.2.2 with org.springdoc:springdoc-openapi-ui:1.2.26

http://localhost:8069/v3/api-docs/swagger-config -> 200 OK
http://localhost:8069/v3/api-docs -> 200 OK
http://localhost:8069/swagger-ui.html -> 302 -> http://localhost:8069/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config -> 404 Not found.

All i did was adding springdoc-openapi-ui to an existing spring boot 2.2.2 application. No configuration done at all.

Ive been reading the docs, but cannot see if im doing something wrong or if this is a bug.

@bnasslahsen
Copy link
Contributor

Hi,

You can have a look at the demos code:

By default, you just need to add the dependency of the springdoc-openapi-ui.

Are you also serving other static content on your spring-boot 2.2.2 application?

If you can provide, a sample code to reproduce, it will be easier to answer.

@Surerus
Copy link

Surerus commented Jan 21, 2020

I was having almost the same issue, but http://localhost:8080//swagger-ui.html was also 404 despite the fact that my spring security configs were correct.

My dependencies were really mixed. I had java 8 running with Spring Boot 2.1.6 and springdoc-openapi latest. When I migrated to java 13 with Spring Boot 2.2.2 everything started to work out of the box!

@seydaz1mov
Copy link

@bnasslahsen why you use springdoc.version: 1.2.28 which is not available from https://mvnrepository.com?

@bnasslahsen
Copy link
Contributor

@trip2gafa, v1.2.28 is already avaialble on maven central:

There is an indexation delay with https://mvnrepository.com, but this is only for search purposes.

Can you please add more details about your request :

  • What modules of springdoc-openapi are you using?
  • Provide with a sample code (HelloController) or Test to reproduce the issue you are describing?

Thank you in advance.

@ghost
Copy link
Author

ghost commented Jan 22, 2020

Im trying to find what causes this. I have been gradually stripping down the application piece by piece, and at one point im able to make it work.
So it seems to be some sort of conflict but im unable to pinpoint it at the moment. Is there some logs we could enable to help us?

@bnasslahsen
Copy link
Contributor

@dahlsdata

You can just enable standard spring-boot logs :

logging.level.web=TRACE
logging.level.org.springframework.web=TRACE

If you don't find the cause, just provide a sample code (HelloController) or Test to reproduce the issue you are describing.

@seydaz1mov
Copy link

@bnasslahsen I am using implementation('org.springdoc:springdoc-openapi-webflux-ui:1.2.27') and SpringBoot 2.2.2:RELEASE

Here is my controller example:

@RestController
@RequestMapping("/bot/v1")
class GetMeController {
    private val log = KotlinLogging.logger { }

    @ApiResponses(value = [ApiResponse(responseCode = "200", description = "get Me")])
    @ResponseStatus(HttpStatus.OK)
    @GetMapping("/getMe")
    fun getMe(principal: Principal): Mono<GetMeResponse> {
        val token = principal as BotAuthenticationToken
        log.info { "is about to answer getMe to bot ${token.id}" }
        return Mono.just(GetMeResponse(token.id, token.botName, token.username))
    }
}

@ghost
Copy link
Author

ghost commented Jan 22, 2020

Hmm, logging now turned on but not getting much wiser. Here is the log for my app where it works fine:

22.01.2020 11:36:39:596 TRACE o.s.web.servlet.DispatcherServlet:88 - - GET "/v3/api-docs/swagger-config", parameters={}, headers={masked} in DispatcherServlet 'dispatcherServlet' 22.01.2020 11:36:39:602 TRACE o.s.w.s.m.m.a.RequestMappingHandlerMapping:409 - - Mapped to org.springdoc.ui.SwaggerWelcome#openapiYaml(HttpServletRequest) 22.01.2020 11:36:39:611 TRACE o.s.w.s.m.m.a.ServletInvocableHandlerMethod:136 - - Arguments: [org.apache.catalina.connector.RequestFacade@3b7a745d] 22.01.2020 11:36:39:624 DEBUG o.s.w.s.m.m.a.RequestResponseBodyMethodProcessor:273 - - Using 'application/json', given [*/*] and supported [application/json] 22.01.2020 11:36:39:625 TRACE o.s.w.s.m.m.a.RequestResponseBodyMethodProcessor:88 - - Writing [{configUrl=/v3/api-docs/swagger-config, oauth2RedirectUrl=http://localhost:8080/swagger-ui/oauth2-redirect.html, url=/v3/api-docs}] 22.01.2020 11:36:39:636 TRACE o.s.web.servlet.DispatcherServlet:1125 - - No view rendering, null ModelAndView returned. 22.01.2020 11:36:39:637 DEBUG o.s.web.servlet.DispatcherServlet:1131 - - Completed 200 OK, headers={masked}

And the log from the app that doesnt work:

22.01.2020 11:35:34:454 TRACE o.s.web.servlet.DispatcherServlet:88 - - GET "/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config", parameters={masked}, headers={masked} in DispatcherServlet 'dispatcherServlet' 22.01.2020 11:35:34:459 TRACE o.s.w.s.h.SimpleUrlHandlerMapping:194 - - Matching patterns [/**/swagger-ui/**, /**] 22.01.2020 11:35:34:460 TRACE o.s.w.s.h.SimpleUrlHandlerMapping:409 - - Mapped to HandlerExecutionChain with [ResourceHttpRequestHandler ["/webjars/"]] and 4 interceptors 22.01.2020 11:35:34:462 DEBUG o.s.w.s.r.ResourceHttpRequestHandler:454 - - Resource not found 22.01.2020 11:35:34:463 TRACE o.s.web.servlet.DispatcherServlet:1125 - - No view rendering, null ModelAndView returned. 22.01.2020 11:35:34:463 DEBUG o.s.web.servlet.DispatcherServlet:1131 - - Completed 404 NOT_FOUND, headers={masked}

I cant see anything in there that helps me further debug this...

Im also having problems assembling a sample for you to reproduce, as i must strip down the application a lot before i could send it somewhere and i havnt been able to do that yet without the issue going away...

@seydaz1mov
Copy link

@bnasslahsen I enabled a logging stack trace and started an app.

And now when I go to localhost:8080/swagger-ui.html, I have this stack trace:

Started BotApiApplicationKt in 35.36 seconds (JVM running for 38.615)
2020-01-22 10:42:21.287 [task-scheduler-thread-1] DEBUG k.b.b.a.s.WebhookUpdateDeleteScheduler - Deleting old updates until 2020-01-21 10:42:21.287.
2020-01-22 10:42:21.289 [task-scheduler-thread-1] DEBUG k.b.b.a.s.WebhookUpdateDeleteScheduler - No old updates until 2020-01-21 10:42:21.287.
2020-01-22 10:42:45.272 [reactor-http-epoll-3] TRACE o.s.w.s.a.HttpWebHandlerAdapter - [e9edd278] HTTP GET "/swagger-ui.html", headers={masked}
2020-01-22 10:42:45.488 [boundedElastic-1] TRACE o.s.w.r.f.server.RequestPredicates - Method "[GET]" matches against value "GET"
2020-01-22 10:42:45.490 [boundedElastic-1] TRACE o.s.w.r.f.server.RequestPredicates - Pattern "/swagger-ui.html" matches against value "/swagger-ui.html"
2020-01-22 10:42:45.491 [boundedElastic-1] TRACE o.s.w.r.f.server.RouterFunctions - [e9edd278] Matched (GET && /swagger-ui.html)
2020-01-22 10:42:45.498 [boundedElastic-1] DEBUG o.s.w.r.f.s.s.RouterFunctionMapping - [e9edd278] Mapped to org.springdoc.ui.SwaggerWelcome$$Lambda$698/0x00000008008a2040@7a682d35
2020-01-22 10:42:45.584 [boundedElastic-1] TRACE o.s.w.s.a.HttpWebHandlerAdapter - [e9edd278] Completed 307 TEMPORARY_REDIRECT, headers={masked}
2020-01-22 10:42:45.584 [boundedElastic-1] TRACE o.s.h.s.r.ReactorHttpHandlerAdapter - [e9edd278] Handling completed
2020-01-22 10:42:45.617 [reactor-http-epoll-3] TRACE o.s.w.s.a.HttpWebHandlerAdapter - [e9edd278] HTTP GET "/webjars/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config", headers={masked}
2020-01-22 10:42:45.623 [boundedElastic-1] TRACE o.s.w.r.f.server.RequestPredicates - Method "[GET]" matches against value "GET"
2020-01-22 10:42:45.624 [boundedElastic-1] TRACE o.s.w.r.f.server.RequestPredicates - Pattern "/swagger-ui.html" does not match against value "/webjars/swagger-ui/index.html"
2020-01-22 10:42:45.625 [boundedElastic-1] TRACE o.s.w.r.f.server.RequestPredicates - Method "[GET]" matches against value "GET"
2020-01-22 10:42:45.625 [boundedElastic-1] TRACE o.s.w.r.f.server.RequestPredicates - Pattern "/v3/api-docs/swagger-config" does not match against value "/webjars/swagger-ui/index.html"
2020-01-22 10:42:45.682 [boundedElastic-1] TRACE o.s.w.r.f.server.RouterFunctions - [e9edd278] Matched org.springframework.boot.autoconfigure.web.reactive.error.DefaultErrorWebExceptionHandler$$Lambda$1352/0x0000000800b40840@58435419
2020-01-22 10:42:45.825 [boundedElastic-1] DEBUG org.springframework.web.HttpLogging - [e9edd278] Resolved [ResponseStatusException: 404 NOT_FOUND "No matching handler"] for HTTP GET /webjars/swagger-ui/index.html
2020-01-22 10:42:45.844 [boundedElastic-1] TRACE o.s.core.codec.CharSequenceEncoder - [e9edd278] Writing "<html><body><h1>Whitelabel Error Page</h1><p>This application has no configured error view, so you are seeing this as a fallback.</p><div id='created'>Wed Jan 22 10:42:45 UTC 2020</div><div>[e9edd278] There was an unexpected error (type=Not Found, status=404).</div><div>No matching handler</div></body></html>"
2020-01-22 10:42:45.894 [reactor-http-epoll-3] TRACE o.s.w.s.a.HttpWebHandlerAdapter - [e9edd278] Completed 404 NOT_FOUND, headers={masked}
2020-01-22 10:42:45.894 [reactor-http-epoll-3] TRACE o.s.h.s.r.ReactorHttpHandlerAdapter - [e9edd278] Handling completed

I will glad if it helps.

PS: There is a thing like Pattern "/v3/api-docs/swagger-config" does not match against value "/webjars/swagger-ui/index.html" in stack trace. Is it says about something?

@bnasslahsen
Copy link
Contributor

@bnasslahsen I am using implementation('org.springdoc:springdoc-openapi-webflux-ui:1.2.27') and SpringBoot 2.2.2:RELEASE

Here is my controller example:

@RestController
@RequestMapping("/bot/v1")
class GetMeController {
    private val log = KotlinLogging.logger { }

    @ApiResponses(value = [ApiResponse(responseCode = "200", description = "get Me")])
    @ResponseStatus(HttpStatus.OK)
    @GetMapping("/getMe")
    fun getMe(principal: Principal): Mono<GetMeResponse> {
        val token = principal as BotAuthenticationToken
        log.info { "is about to answer getMe to bot ${token.id}" }
        return Mono.just(GetMeResponse(token.id, token.botName, token.username))
    }
}

The attached sample code uses exactly your description and is working fine:
SpringBoot 2.2.2:RELEASE with Kotlin and springdoc-openapi-webflux-ui:1.2.27.

demo.zip

@seydaz1mov
Copy link

@bnasslahsen can you add examples by using gradle, also? Or it does not make a sense?

@bnasslahsen
Copy link
Contributor

@bnasslahsen can you add examples by using gradle, also? Or it does not make a sense?

There are no short term plans for adding examples using gradle.
But you can contribute if you are interested.

@ghost
Copy link
Author

ghost commented Jan 22, 2020

I am using gradle btw

@seydaz1mov
Copy link

I am using gradle btw

I am too)

@seydaz1mov
Copy link

The interesting fact is http://localhost:8080/v3/api-docs works. There is something in a project which serves as an obstacle to show the swagger UI documentation page (http://localhost:8080/webjars/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config).

@seydaz1mov
Copy link

Pattern "/v3/api-docs/swagger-config" does not match against value "/webjars/swagger-ui/index.html" @bnasslahsen is it says about something?

@seydaz1mov
Copy link

http://localhost:8080/v3/api-docs works, but http://localhost:8080/v3/api-docs/swagger-config does not.

@bnasslahsen
Copy link
Contributor

Pattern "/v3/api-docs/swagger-config" does not match against value "/webjars/swagger-ui/index.html" @bnasslahsen is it says about something?

No. just warning

@bnasslahsen
Copy link
Contributor

I am using gradle btw

I am too)

The working example with gradle as well :)

demo-gradle.zip

@seydaz1mov
Copy link

@bnasslahsen o.s.w.r.f.server.RouterFunctions - [0aba8e13] Matched org.springframework.boot.autoconfigure.web.reactive.error.DefaultErrorWebExceptionHandler$$Lambda$1333/0x0000000800b2b840@585e9940 What can be a cause of this warning?

@bnasslahsen
Copy link
Contributor

DefaultErrorWebExceptionHandler

This is your real exception, and not a warning. You have to debug, to find out the cause.
But you can compare with the sample code i have attached, this exception does not occur.

@ghost
Copy link
Author

ghost commented Jan 22, 2020

Ok finally got this issue nailed down! It is classpath releated and it seems as it is related to a very large classpath.

So in intellij (im using IntelliJ) i had set run-configuration shorten command line to "jar manifest". And then it (swagger-ui) does not work. (but application starts fine)

Running with "none" as shorten command line also does not work as the application will not start at all then.

Also when running from gradle "bootRun" it seems as gradle will use some sort of command-line shortening that results in application startup, but swagger-ui not working.

But then if i change to "@argFiles (java9+)" in intellij run setting, then it works from IntelliJ, both application and swagger-UI.

But from gradle bootRun it will still not work.

With a smaller project, that does not need command line shortening, it also works from gradle bootRun.

@ghost ghost closed this as completed Jan 22, 2020
@s8v-nurb0L
Copy link

@dahlsdata did you have problems with DefaultErrorWebExceptionHandler?

@ghost
Copy link
Author

ghost commented Jan 24, 2020

@dahlsdata did you have problems with DefaultErrorWebExceptionHandler?

No, i cannot recall any DeafultErrorWebExceptionHandler.
My problems where related to the swagger ui resources (webjars) not being found.

@alabotski
Copy link

Seems I found problem
I have such annotation - @EnableWebFlux

    implementation 'org.springdoc:springdoc-openapi-webflux-core'
    implementation 'org.springdoc:springdoc-openapi-webflux-ui'

@SebasWebDev
Copy link

Looking at the swagger-ui dependency I saw that the index.html is nested under resources/webjars/swagger-ui/3.25.0
So I was able to make it work with:

registry.addResourceHandler("/swagger-ui/**")
            .addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/3.25.0/");

@ahmetgeymen
Copy link

ahmetgeymen commented Apr 13, 2020

@SebasWebDev

Even it is mentioned here #402 (comment) just adding @EnableWebFlux annotation only near any @configuration, triggers the SwaggerConfig.addResourceHandlers() inside springdoc-openapi-webflux-ui.

In this case I didn't have to add resource handler manually by myself. But without @EnableWebFlux I couldn't succeed any further.

@SebasWebDev
Copy link

@ahmetgeymen I saw that issue. The suggested solution did not work for me, because I was not using web flux.

@rohitdev
Copy link

rohitdev commented Aug 3, 2020

Adding this to my pom.xml helped . Eclipse+Spring webmvc

{NAME_YOUR_PROJECT} org.springframework.boot spring-boot-maven-plugin build-info

@frankcrc
Copy link

Looking at the swagger-ui dependency I saw that the index.html is nested under resources/webjars/swagger-ui/3.25.0
So I was able to make it work with:

registry.addResourceHandler("/swagger-ui/**")
            .addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/3.25.0/");

@SebasWebDev Thanks.
Your solution leads me find that i can directly input http:// xxxx/webjars/swagger-ui/{version}/index.html in browser, which {version} can 3.25.0 or other. the version correspons to swagger-ui-{version}.jar's {version}.

In swagger-ui-{version}.jar, we can see index.html in directory META-INF/resources/webjars/swagger-ui/{version}/. So according to SebasWebDav's solution, /swagger-ui/index.html will redirect to /webjars/swagger-ui/{version}/index.html.

In my case, I can access /swagger-ui/index.html, but I cannot browse /swagger-ui.html and then redirect to /swagger-ui/index.html. So I have to browse /swagger-ui/index.html, and input api docs's path, each time, in the top textbox, for example, /v3/api-docs, except when i browse /swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config. This isn't friendly.

For a new springboot project, integrated with swagger 3, the default behavior is ,Browse /swagger-ui.html, and then redirect to /swagger-ui/index.html?configUrl=xxx&abc=def.

Finally, i found org.springdoc.webmvc.ui.SwaggerWebMvcConfigurer in springdoc-openapi-ui-{version2}.jar, and there is a addResourceHandlers to do what SebasWebDav mentions. So when /swagger-ui/index.html return 404, i think it's worth checking whether SwaggerWebMvcConfigurer.addResourceHandlers is called, and if not, why.

In my case, i code like the following,
@Configuration public class MyConfig extends WebMvcConfigurationSupport {...}
When I remove this, framkwork will use DelegatingWebMvcConfiguration instead, which will indirectly call SwaggerWebMvcConfigurer.addResourceHandlers.

Finally I changed to the following,
@Configuration public class MyConfig extends WebMvcConfigurerAdapter{...}

Hope it helpful.

@acc15
Copy link

acc15 commented Dec 17, 2020

Also face this issue.
Its Idea build issue.

IDEA doesnt see webjars-locator-core dependency.

However its present in dependency tree

    [INFO] +- org.springdoc:springdoc-openapi-ui:jar:1.5.1:compile
    [INFO] |  +- org.springdoc:springdoc-openapi-webmvc-core:jar:1.5.1:compile
    [INFO] |  |  \- org.springdoc:springdoc-openapi-common:jar:1.5.1:compile
    [INFO] |  |     +- io.swagger.core.v3:swagger-models:jar:2.1.6:compile
    [INFO] |  |     +- io.swagger.core.v3:swagger-annotations:jar:2.1.6:compile
    [INFO] |  |     +- io.swagger.core.v3:swagger-integration:jar:2.1.6:compile
    [INFO] |  |     |  \- io.swagger.core.v3:swagger-core:jar:2.1.6:compile
    [INFO] |  |     |     +- com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:jar:2.11.2:compile
    [INFO] |  |     |     \- jakarta.validation:jakarta.validation-api:jar:2.0.2:compile
    [INFO] |  |     +- io.github.classgraph:classgraph:jar:4.8.69:compile
    [INFO] |  |     \- org.apache.commons:commons-lang3:jar:3.10:compile
    [INFO] |  +- org.webjars:swagger-ui:jar:3.37.2:compile
    [INFO] |  \- org.webjars:webjars-locator-core:jar:0.45:compile
    [INFO] |     \- com.fasterxml.jackson.core:jackson-core:jar:2.11.2:compile

Simplest solution is to delete .idea directory and all .iml files.
Re-import from scratch. Ensure that webjars-locator-core will be in External Libraries

Or add this dependency directly to your pom / gradle build

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>webjars-locator-core</artifactId>
    <version>0.46</version>
</dependency>

Why IDEA doesn't see it - very strange - idk.

@mmahmoodictbd
Copy link

Simplest solution is to delete .idea directory and all .iml files.

This worked for me.

@dvivarelli
Copy link

dvivarelli commented Apr 3, 2021

Using the current version (1.5.6), i need to override the method in WebMvcConfigurationSupport in the following way:

    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/3.45.0/");

    }

without it, spring boot returns 404

@bnasslahsen
Copy link
Contributor

@dvivarelli,

If you are having issues, this means your project is not well configured.

You have many demos available, depending on your context.
The configuration you are mentioning is not required at all.

You can follow the guidance, section Using GitHub Issues, if you need help.
Make sure you provide all the listed information, to speed up your problem diagnosis.

@springdoc springdoc locked as too heated and limited conversation to collaborators Apr 3, 2021
@bnasslahsen bnasslahsen added the question Further information is requested label Jan 10, 2022
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests