如何在swagger中将<host:port>更改为domain

时间:2016-10-18 02:54:54

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

enter image description here

图片中的

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. 怎么解决呢?

2 个答案:

答案 0 :(得分:2)

@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()方法!!!

答案 1 :(得分:0)

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

e.g。

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