【问题标题】:Change Swagger UI request URL when I tap to "Execute" button当我点击“执行”按钮时更改 Swagger UI 请求 URL
【发布时间】:2020-02-13 10:43:11
【问题描述】:

我在 AWS 上部署了我的应用程序。另外,我配置了 Nginx,/api 前缀(https://mywebsitename.com/api)将自动重定向到https://127.0.0.1:8080/

每个 HTTP 请求和响应都能完美运行。

另外,我将 swagger 库添加到我的应用程序中,配置如下:

@Configuration
@EnableSwagger2
class SwaggerConfig {
    @Bean
    fun api(): Docket {
        return Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any())
            .build()
    }
}

我使用这两个库:

implementation("io.springfox:springfox-swagger2:2.9.2")
implementation("io.springfox:springfox-swagger-ui:2.9.2")

一切都在本地完美运行。

问题是: 招摇链接 (https://mywebsitename.com/api/swagger-ui.html) 完美运行,一切都显示出来了。但是,当我点击“试用”按钮并进行“执行”时,它返回给我TypeError: Failed to fetch。这是因为请求转到https://127.0.0.1:8080/some_endpoint

问题是:如何配置我的 Spring Boot 应用程序,当我点击“执行”按钮时,请求将转到 https://mywebsitename.com/api/some_end_point

【问题讨论】:

    标签: spring-boot kotlin jvm swagger swagger-ui


    【解决方案1】:

    您是否尝试过像这样在 Swagger 配置中添加 host 调用:

    @Configuration
    @EnableSwagger2
    class SwaggerConfig {
        @Bean
        fun api(): Docket {
            return Docket(DocumentationType.SWAGGER_2)
    
                .host("https://mywebsitename.com")
    
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
        }
    }
    

    【讨论】:

    • 我试过了,但请求转到 Nginx 并返回给我 301 Moved Permanently
    【解决方案2】:

    您可以尝试在 Swagger 配置中添加 服务器,如下所示:

    @Bean
    public Docket api() {
        Server serverLocal = new Server("local", "http://localhost:8080", "for local usages", Collections.emptyList(), Collections.emptyList());
        Server testServer = new Server("test", "https://example.org", "for testing", Collections.emptyList(), Collections.emptyList());
        return new Docket(DocumentationType.SWAGGER_2)
                .servers(serverLocal, testServer)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-25
      • 2017-11-04
      • 2021-11-28
      相关资源
      最近更新 更多