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 error with spring doc open api 2.1.0 #2204

Closed
nidhikaushal opened this issue Apr 13, 2023 · 4 comments
Closed

404 error with spring doc open api 2.1.0 #2204

nidhikaushal opened this issue Apr 13, 2023 · 4 comments

Comments

@nidhikaushal
Copy link

nidhikaushal commented Apr 13, 2023

Hi,
I am facing a similar issue. On accessing https://somecomp.com/service1/swagger-ui.html it redirects to https://somecomp.com/service1/swagger/index.html and then returns error 404.
I am using 'spring-boot-starter-webflux:3.0.3' with 'springdoc-openapi-starter-webflux-ui:2.1.0'. Following is the configuration in my application.yml:

`springdoc:
packagesToScan: com.somecomp
swagger-ui:
defaultModelsExpandDepth: -1
path: "/swagger-ui.html"
webjars:
prefix: /service1

Is there a way to disable the redirection and render swagger from https://somecomp.com/service1/swagger-ui.html

@nidhikaushal
Copy link
Author

nidhikaushal commented Apr 14, 2023

So the problem is, my service sits behind API Gateway, when the request is routed to my service the url used for routing is https://somecomp.com/swagger-ui.html hence when the swagger redirects it redirects to 'https://somecomp.com/webjars/swagger-ui/index.html'. The request goes back to the api gateway and since the service is missing the routing fails.

Now I have created a WebFilter to add the service1 explicitly for requests that have uri as '/webjars/swagger-ui'.

But this still does not work and swagger-config is failing :
springdoc:
api-docs:
path: /service1/v3/api-docs
packagesToScan: com.somecomp
swagger-ui:
defaultModelsExpandDepth: -1
path: "/swagger-ui.html"
configUrl: /service1/v3/api-docs/swagger-config
webjars:
prefix: /service1/webjars

I have added the same logic to append the service name in case of uri as /v3/api-docs/swagger-config and /v3/api-docs.
But now petstore apis are visible instead of showing the apis exposed by my service

If I use the configuration
springdoc.swagger-ui.disable-swagger-default-url: true
Then I get no api definition provided

It worked after adding springdoc.swagger-ui.url: /service1/v3/api-docs

@matt-hires
Copy link

matt-hires commented Apr 17, 2023

For me this issue was resolved by adding:

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

The locator was removed with the following commit:
bcaa2577

Unfortunately I was not able to understand why this was done, maybe to not depend on the locator directly but this is just an assumption.

Anyways this was necessary that the resource /swagger-ui/index.html could be resolved in:

public Resource resolveResource(HttpServletRequest request, String requestPath, List<? extends Resource> locations, ResourceResolverChain chain) {
Resource resolved = chain.resolveResource(request, requestPath, locations);
if (resolved == null) {
String webJarResourcePath = findWebJarResourcePath(requestPath);
if (webJarResourcePath != null)
return chain.resolveResource(request, webJarResourcePath, locations);
}
return resolved; }

The application is now using the following dependencies:

<dependency>
	<groupId>org.springdoc</groupId>
	<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
	<version>2.1.0</version>
</dependency>

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

if you're using wildfly use the following locator:

   <dependency>
     <groupId>org.webjars</groupId>
     <artifactId>webjars-locator-jboss-vfs</artifactId>
     <version>0.1.0</version>
   </dependency>

https://springdoc.org/v2/#integration-with-wildfly

@nidhikaushal
Copy link
Author

Hi @matt-hires , I tried the above but it does not solve my problem. If there is any way by which swagger ui renders without redirection probably that would work best, but the above workaround I have put in place also works.

@mschout
Copy link

mschout commented Dec 29, 2023

In case it helps anyone here, I ran into this when upgrading to Spring Boot 3.2.0. Suddenly /swagger-ui/index.html no longer worked with 404 due to missing index.html. Solution was to explicitly add this dependency back:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants