【问题标题】:How to change swagger-ui.html default path如何更改 swagger-ui.html 默认路径
【发布时间】:2019-12-03 05:16:06
【问题描述】:

我想将我的 swagger-ui 路径从 localhost:8080/swagger-ui.html 更改为 localhost:8080/myapi/swagger-ui.htmlspringboot 重定向对我很无助

【问题讨论】:

标签: spring-boot swagger-ui swagger-2.0


【解决方案1】:

我为自己找到了几种可能的解决方案。也许它会对其他人有所帮助。


直接设置springdoc.swagger-ui.path

直接的方法是设置属性springdoc.swagger-ui.path=/custom/path。如果您可以在应用程序中硬编码swagger 路径,它将完美运行。


覆盖springdoc.swagger-ui.path 属性

您可以使用ApplicationListener<ApplicationPreparedEvent> 以编程方式更改默认swagger-ui 路径。这个想法很简单 - 在 Spring Boot 应用程序启动之前覆盖 springdoc.swagger-ui.path=/custom/path

@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 应用程序的重定向代码:

@RestController
公共类 SwaggerEndpoint {

    @GetMapping("/自定义/路径")
    公共 Mono api(ServerHttpResponse 响应) {
        response.setStatusCode(HttpStatus.PERMANENT_REDIRECT);
        response.getHeaders().setLocation(URI.create("/swagger-ui.html"));
        返回 response.setComplete();
    }
}

这种方法的问题 - 如果您通过地址 "/swagger-ui.html" 调用它,您的服务器仍然会响应。

【讨论】:

    【解决方案2】:

    可以在application.properties中修改springfox属性

    例如,编辑base-url

    springfox.documentation.swagger-ui.base-url=documentation
    

    例如将其设置为 /documentation 会将 swagger-ui 置于 /documentation/swagger-ui/index.html

    【讨论】:

      【解决方案3】:

      如果由于某种原因您不想重定向到/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】:

        在Spring Boot的application.properties中

        springdoc.swagger-ui.path=/swagger-ui-custom.html
        

        在你的情况下是

        springdoc.swagger-ui.path=/myapi/swagger-ui.html
        

        【讨论】:

        • 嘿!这对我不起作用。你知道是否有任何其他地方优先,或者我是否可能需要启用某些东西?
        【解决方案5】:

        如果你想添加,例如,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:

        
        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")
        

        【讨论】:

          【解决方案6】:

          如果您使用的是 Spring Boot,请更新 application.properties 文件并写在这里

          server.servlet.context-path=/myapi
          

          它会根据你的需要重定向你。

          【讨论】:

          • 如果我错了,请纠正我,但这会将 /myapi 添加到所有路径,即它还会将任何 REST 控制器从 /myapi/something/something2 更改为 /myapi/myapi/something/something2。
          猜你喜欢
          • 2019-10-29
          • 1970-01-01
          • 2010-09-17
          • 2021-06-19
          • 2015-11-12
          • 1970-01-01
          • 1970-01-01
          • 2016-03-01
          • 2016-07-09
          相关资源
          最近更新 更多