【问题标题】:how to change <host:port> to domain in swagger如何在招摇中将 <host:port> 更改为域
【发布时间】:2016-10-18 02:54:53
【问题描述】:

图中swagger请求使用主机和端口,所以浏览器控制台XMLHttpRequest cannot load http://115.159.22.159:9001/bp/api/v1/user/1. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://twogoods.cc' is therefore not allowed access. 如何解决?

【问题讨论】:

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


    【解决方案1】:
    @Bean
    public Docket categoryApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .host("twogoods.cc")
                .groupName("bookplatform-api")
                .apiInfo(apiInfo())
                .select()
                .paths(apiPaths())
                .build()
                .directModelSubstitute(java.sql.Timestamp.class, java.sql.Date.class)
                .enableUrlTemplating(false);
    }
    

    host()方法!!!

    【讨论】:

    • 现在我知道 host() 方法可以更改“试用”URL,但我还有一个问题:如何自动获取主机(如 HttpServletRequest.getServerName( ) 而不是手动输入?而且我不期待配置中的特定主机。
    【解决方案2】:

    您可以使用host(..)protocols(..) 方法来覆盖默认值。

    例如

    @Bean
    public Docket customImplementation() {
        return new Docket(DocumentationType.SWAGGER_2)
            .protocols(Collections.singleton("https"))
            .host("twogoods.cc")
            .select()
            .build();
    }
    

    【讨论】:

    • 不知道是否可以动态完成,例如根据实际域名选择主机名,这样部署到各种环境时不必做任何更改?
    • @Joey587 - 不确定,但您可能有三个选项(按可行性排序,*未测试): a) 使用环境变量从外部配置域名(在启动时)。 b) 延迟初始化Docket bean,并使用收到的第一个Host 标头来配置bean c) 获取外部IP 地址并进行反向查找(这里是dragons)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-23
    • 1970-01-01
    • 2014-06-11
    • 1970-01-01
    • 1970-01-01
    • 2019-04-01
    相关资源
    最近更新 更多