我想将 swagger-ui 的路径从localhost:8080/swagger-ui.html
更改为
localhost:8080/myapi/swagger-ui.html
中的springboot
重定向对我无助
答案 0 :(得分:0)
如果您使用的是Spring Boot,请更新 application.properties文件并在此处写入
server.servlet.context-path=/myapi
它将根据需要重定向您。
答案 1 :(得分:0)
例如,如果要添加documentation
前缀-您可以对路径http://localhost:8080/documentation/swagger-ui.html
这样做:
kotlin
@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
中的
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
:
implementation("io.springfox:springfox-swagger2:$swagger")
implementation("io.springfox:springfox-swagger-ui:$swagger")
implementation("io.springfox:springfox-bean-validators:$swagger")
答案 2 :(得分:0)
在Spring Boot的application.properties中
springdoc.swagger-ui.path=/swagger-ui-custom.html
根据您的情况将是
springdoc.swagger-ui.path=/myapi/swagger-ui.html
答案 3 :(得分:0)
如果由于某种原因您不想重定向到 /swagger-ui.html
,您可以将自己的内容设置为主页视图,在 resources/static/index.html 设置一个 index.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
答案 4 :(得分:0)
您可以在 application.properties 中修改 springfox 属性
例如编辑base-url
springfox.documentation.swagger-ui.base-url=documentation
例如将其设置为 /documentation
会将 swagger-ui 置于 /documentation/swagger-ui/index.html