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
How to serve swagger-ui.html at more convenient URL? #2250
Comments
I didnt changed swagger-ui html configuration but add request mapping for the redirection to swagger-ui.html //...in controller...
@RequestMapping("/swagger")
public String greeting() {
return "redirect:/swagger-ui.html";
} |
Perfect! (..well, almost as redirect is a workaround). Thanks @nobe0716 . For posterity I will expand on your answer to show how to prevent the redirect method from showing up in the UI: Controller: note the classpath package io.bisq.engine.app; //classpath outside of the REST controllers
...
@Controller //note - this is a spring-boot controller, not @RestController
public class HomeController {
@RequestMapping ("/swagger")
public String home() {
return "redirect:/swagger-ui.html";
}
} Config package io.bisq.engine.app;
...
@Configuration
@EnableSwagger2
public class SpringConfig {
@Bean
public Docket api(){
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage( "io.bisq.engine.app.api" )) //classpath for the REST controllers
.paths(PathSelectors.any())
.build();
}
} |
@citkane the swagger-ui bundled with the project is only a convenience. It jumpstarts 80% of the use cases. In this instance the customization is simple. For cases where customizations are more involved the prescribed solution is to download swagger-ui into the appropriate folder in your project and configure it as you desire and not use the bundled swagger-ui. Its not really a work around. The |
I tried the approach by @citkane , but it seems not working in a WebFlux context. java.lang.IllegalStateException: Could not resolve view with name 'redirect:/swagger-ui.html'. |
Thanks @nobe0716 After running it, you can access http://localhost:8080/swagger-ui.html#/ and see it works. However, if you try access http://localhost:8080/swagger you will see the error callstack I mentioned before. |
Thanks for helping out @nobe0716 |
@zpf7879 I wonder why "redirect:swagger-ui.html" didn't work, but found alternative way. Try adding RouterFunction bean in your applicationScope. @SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
@Bean
RouterFunction<ServerResponse> routerFunction() {
return route(GET("/swagger"), req ->
ServerResponse.temporaryRedirect(URI.create("swagger-ui.html")).build());
}
} |
@nobe0716 , thanks for your time and help. I also got it work in a different approach. @Component
public class SwaggerWebFilter implements WebFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
ServerHttpRequest request = exchange.getRequest();
String path = request.getPath().pathWithinApplication().value();
if (path.startsWith("/api/swagger-ui.html") || path.startsWith("/api/webjars")
|| path.startsWith("/api/api-docs") || path.startsWith("/api/configuration")
|| path.startsWith("/api/swagger-resources") || path.startsWith("/api/v2"))) {
exchange = exchange.mutate().request(request.mutate().path(path.substring(4)).build()).build();
}
return chain.filter(exchange);
}
}
` |
Hi, |
My server does not allow anything to be requested directly under In short: Any request matchin I really need swagger to run under something like |
Is there a sample of bundling customized swagger ui into a springboot project? |
Redirects are workarounds that only work in certain cases. In micro services architecture chances are redirects won't work. |
@silentsnooc were you able to achieve your goal? |
With 3.0.0 this becomes a lot simpler... swagger-ui is always served off where |
Hi @dilipkrish , I am new to springfox, can you give me an example which shows how to use property source for changing location of swagger-resources, especially /v3/api-docs? http://springfox.github.io/springfox/docs/current/#customizing-the-swagger-endpoints |
If you're using |
You're clearly don't doing something expected. If you bring in the boot starter you shouldn't have to do anything else like adding additional dependencies. However thanks for catching the missed property (the error that's shown on your screenshot) that wasn't included in the configuration properties. Trying setting this property You can ignore the error you've seeing above, its because the configuration property is not described. I've added a story 👇 to fix that. |
springdoc.swagger-ui.path=/swagger-ui-custom.html |
I also get the IDE errors about the missing config properties, however this configuration let me move the Swagger endpoints somewhere else: springfox:
documentation:
swaggerUi:
baseUrl: /documentation
openApi:
v3:
path: /documentation/v3/api-docs
swagger:
v2:
path: /documentation/v2/api-docs |
Thanks @dilipkrish @Quantas . Above suggestions helped. Do you have any idea if that is the reason or is there something else to look for? |
@prafful13 Yes this will move all the resources and web-jars below the path you specify for |
API gateways are tricky and depends on how you've configured it, and if you're passing along X-FORWARD headers and if spring is configured to use them or not. In general, @Quantas solution will work of you set it up correctly. |
Hi @dilipkrish , not sure what I'm doing wrong, but I wanted to move the base url of swagger-ui to other location together with api-docs. I'm fine with swagger2, but don't mind OAS3. Looks like swagger-ui is trying to find api-docs on wrong location. My error (when I access http://localhost:7971/test-app/documentation/swagger-ui/): My setup: But these are working fine: I have following dependencies: and configuration annotations (no matter if I have them or not, result is the same): Works only if:
any ideas please? :( |
I want to be able to browse to
http://localhost:8080/swagger
instead ofhttp://localhost:8080/swagger-ui.html
to access the UI, but I am struggling to find documentation or examples.Could somebody please confirm if this is possible and help if so?
I am on:
I have seen these, but remain confused:
#1406
#1263
http://springfox.github.io/springfox/docs/current/#springfox-swagger2-with-spring-mvc-and-spring-boot
The doc say:
But this seems not reasonable for such a basic convenience setting.
The text was updated successfully, but these errors were encountered: