我想在springboot重定向中将我的swagger-ui路径从localhost:8080/swagger-ui.html更改为localhost:8080/myapi/swagger-ui.html,但我对此无能为力
springboot
localhost:8080/swagger-ui.html
localhost:8080/myapi/swagger-ui.html
bt1cpqcv1#
在application.properties Boot 的www.example.com
springdoc.swagger-ui.path=/swagger-ui-custom.html
对你来说
springdoc.swagger-ui.path=/myapi/swagger-ui.html
mwyxok5s2#
如果出于某种原因,您不想重定向到/swagger-ui.html,您可以将自身内容设置为主视图,在resources/static/index.html中设置index.html:
/swagger-ui.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Welcome to another awesome Microservice</title> </head> <body> <script> document.body.innerHTML = '<object type="text/html" data="/swagger-ui.html" style="overflow:hidden;overflow-x:hidden;overflow-y:hidden;height:100%;width:100%;position:absolute;top:0px;left:0px;right:0px;bottom:0px"></object>'; </script> </body> </html>
然后访问您的http://localhost:8080/,您将看到您的swagger文档。最后,您可以使用以下命令自定义路径和.html文件:
registry.addViewController("/swagger").setViewName("forward:/index.html");
就像暗示this answer
xv8emn3q3#
您可以在www.example.com中修改springfox属性application.properties例如,要编辑base-url
springfox.documentation.swagger-ui.base-url=documentation
例如,将其设置为/documentation将使swagger-ui位于/documentation/swagger-ui/index.html
/documentation
/documentation/swagger-ui/index.html
ldxq2e6h4#
例如,如果要添加documentation前缀-可以对路径http://localhost:8080/documentation/swagger-ui.html执行以下操作:Kotlin
documentation
http://localhost:8080/documentation/swagger-ui.html
@Configuration @EnableSwagger2 @ConfigurationPropertiesScan("your.package.config") @Import(value = [BeanValidatorPluginsConfiguration::class]) class SwaggerConfiguration( private val swaggerContactProp: SwaggerContactProp, private val swaggerProp: SwaggerProp ) : WebMvcConfigurationSupport() { // https://springfox.github.io/springfox/docs/current/ @Bean fun api(): Docket = Docket(DocumentationType.SWAGGER_2) .groupName("Cards") .apiInfo(getApiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("your.controllers.folder")) .paths(PathSelectors.any()) .build() private fun getApiInfo(): ApiInfo { val contact = Contact(swaggerContactProp.name, swaggerContactProp.url, swaggerContactProp.mail) return ApiInfoBuilder() .title(swaggerProp.title) .description(swaggerProp.description) .version(swaggerProp.version) .contact(contact) .build() } override fun addViewControllers(registry: ViewControllerRegistry) { with(registry) { addRedirectViewController("/documentation/v2/api-docs", "/v2/api-docs").setKeepQueryParams(true) addRedirectViewController( "/documentation/swagger-resources/configuration/ui", "/swagger-resources/configuration/ui" ) addRedirectViewController( "/documentation/swagger-resources/configuration/security", "/swagger-resources/configuration/security" ) addRedirectViewController("/documentation/swagger-resources", "/swagger-resources") } } override fun addResourceHandlers(registry: ResourceHandlerRegistry) { registry.addResourceHandler("/documentation/**").addResourceLocations("classpath:/META-INF/resources/") } } @ConfigurationProperties(prefix = "swagger") @ConstructorBinding data class SwaggerProp(val title: String, val description: String, val version: String) @ConfigurationProperties(prefix = "swagger.contact") @ConstructorBinding data class SwaggerContactProp(val mail: String, val url: String, val name: String)
以及在applicatiom.yml中:
applicatiom.yml
swagger: title: Cards version: 1.0 description: Documentation for API contact: mail: email@gmail.com url: some-url.com name: COLABA card
另外,不要忘记在build.gradle.kts中添加:
build.gradle.kts
implementation("io.springfox:springfox-swagger2:$swagger") implementation("io.springfox:springfox-swagger-ui:$swagger") implementation("io.springfox:springfox-bean-validators:$swagger")
qaxu7uf25#
我已经为自己找到了几个可能的解决方案。也许这对别人会有帮助。
springdoc.swagger-ui.path
直接的方法是设置springdoc.swagger-ui.path=/custom/path属性。如果你能在你的应用程序中硬编码swagger路径,它将完美地工作。
springdoc.swagger-ui.path=/custom/path
swagger
您可以使用ApplicationListener<ApplicationPreparedEvent>以编程方式更改默认的swagger-ui路径。
ApplicationListener<ApplicationPreparedEvent>
swagger-ui
@Component public class SwaggerConfiguration implements ApplicationListener<ApplicationPreparedEvent> { @Override public void onApplicationEvent(final ApplicationPreparedEvent event) { ConfigurableEnvironment environment = event.getApplicationContext().getEnvironment(); Properties props = new Properties(); props.put("springdoc.swagger-ui.path", swaggerPath()); environment.getPropertySources() .addFirst(new PropertiesPropertySource("programmatically", props)); } private String swaggerPath() { return "/swagger/path"; //todo: implement your logic here. } }
在这种情况下,必须在应用程序启动之前注册侦听器:
@SpringBootApplication @OpenAPIDefinition(info = @Info(title = "APIs", version = "0.0.1", description = "APIs v0.0.1")) public class App { public static void main(String[] args) { SpringApplication application = new SpringApplication(App.class); application.addListeners(new SwaggerConfiguration()); application.run(args); } }
您也可以注册自己的控制器,并按照建议进行简单的重定向there。Spring WebFlux应用程序的重定向代码:
Spring WebFlux
@RestController public class SwaggerEndpoint { @GetMapping("/custom/path") public Mono<Void> api(ServerHttpResponse response) { response.setStatusCode(HttpStatus.PERMANENT_REDIRECT); response.getHeaders().setLocation(URI.create("/swagger-ui.html")); return response.setComplete(); } }
这种方法的问题是-如果您通过地址"/swagger-ui.html"调用服务器,它仍然会响应。
"/swagger-ui.html"
dwbf0jvd6#
你可以用这个代码,它对我很有效
package com.swagger.api.redirect; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; public class SwaggerApiReDirector implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addRedirectViewController("/documentation/v2/api-docs", "/v2/api-docs"); registry.addRedirectViewController("/documentation/configuration/ui", "/configuration/ui"); registry.addRedirectViewController("/documentation/configuration/security", "/configuration/security"); registry.addRedirectViewController("/documentation/swagger-resources", "/swagger-resources"); registry.addRedirectViewController("/documentation/swagger-resources/configuration/ui", "/swagger-resources/configuration/ui"); registry.addRedirectViewController("/documentation", "/documentation/swagger-ui.html"); registry.addRedirectViewController("/documentation/", "/documentation/swagger-ui.html"); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry .addResourceHandler("/documentation/**").addResourceLocations("classpath:/META-INF/resources/"); } }
hjqgdpho7#
如果您使用的是 Spring Boot ,请更新application.properties文件并在此处写入
server.servlet.context-path=/myapi
它将根据您需要重新定向您。
7条答案
按热度按时间bt1cpqcv1#
在application.properties Boot 的www.example.com
对你来说
mwyxok5s2#
如果出于某种原因,您不想重定向到
/swagger-ui.html
,您可以将自身内容设置为主视图,在resources/static/index.html中设置index.html:然后访问您的http://localhost:8080/,您将看到您的swagger文档。
最后,您可以使用以下命令自定义路径和.html文件:
就像暗示this answer
xv8emn3q3#
您可以在www.example.com中修改springfox属性application.properties
例如,要编辑base-url
例如,将其设置为
/documentation
将使swagger-ui位于/documentation/swagger-ui/index.html
ldxq2e6h4#
例如,如果要添加
documentation
前缀-可以对路径http://localhost:8080/documentation/swagger-ui.html
执行以下操作:Kotlin
以及在
applicatiom.yml
中:另外,不要忘记在
build.gradle.kts
中添加:qaxu7uf25#
我已经为自己找到了几个可能的解决方案。也许这对别人会有帮助。
直接设置
springdoc.swagger-ui.path
直接的方法是设置
springdoc.swagger-ui.path=/custom/path
属性。如果你能在你的应用程序中硬编码swagger
路径,它将完美地工作。重写
springdoc.swagger-ui.path
属性您可以使用
ApplicationListener<ApplicationPreparedEvent>
以编程方式更改默认的swagger-ui
路径。在这种情况下,必须在应用程序启动之前注册侦听器:
使用控制器重定向
您也可以注册自己的控制器,并按照建议进行简单的重定向there。
Spring WebFlux
应用程序的重定向代码:这种方法的问题是-如果您通过地址
"/swagger-ui.html"
调用服务器,它仍然会响应。dwbf0jvd6#
你可以用这个代码,它对我很有效
hjqgdpho7#
如果您使用的是 Spring Boot ,请更新application.properties文件并在此处写入
它将根据您需要重新定向您。