2016-06-13 116 views

回答

8

將您的swagger配置放入單獨的配置類中,並使用@Profile註釋進行註釋 - >以便僅在特定配置文件中將其掃描到Spring上下文中。

例子:

@Configuration 
@EnableSwagger2 
@Profile("dev") 
public class SwaggerConfig { 
    // your swagger configuration 
} 

可以比定義配置您的春天啓動的應用程序通過命令行中運行:通過配置文件--spring.profiles.active=dev或:spring.profiles.active=dev

Read this section of Spring Boot docs for more info about @Profile

+4

我們已經做到了這一點,它看來,擴展 - > swagger-ui.html仍然顯示,即使api的內容沒有顯示。有沒有辦法做到這樣,swagger-ui.html甚至不會生成? – user301693

+0

@ user301693如果您使用的是Maven,您可以在特定的Maven配置文件中加載Swagger依賴關係,這應該可以做到這一點。 – g00glen00b

+1

@ g00glen00b,並且對於PROD,與其他環境有不同的工件?我想QA和OPS的人不會對此感到高興。 – luboskrnac

1

這是我的配置類:

@Configuration 
@Profile("swagger") 
@EnableSwagger2 
public class SwaggerConfig { 

    @Value("${info.build.version}") 
    private String buildVersion; 

    @Bean 
    public Docket documentation() { 
     return new Docket(DocumentationType.SWAGGER_2) 
       .select() 
       .apis(RequestHandlerSelectors.any()) 
       .paths(regex("/rest/.*")) 
       .build() 
       .pathMapping("/") 
       .apiInfo(metadata()); 
    } 

    private ApiInfo metadata() { 
     return new ApiInfoBuilder() 
       .title("API documentation of our App") 
       .description("Use this documentation as a reference how to interact with app's API") 
       .version(buildVersion) 
       .contact(new Contact("Dev-Team", "https://dev-website", "[email protected]")) 
       .build(); 
    } 
} 

無論我需要揚鞭,我的個人資料swagger添加到環境變量SPRING_PROFILES_ACTIVE

+1

這實質上是重複[另一個更老的答案](https://stackoverflow.com/a/37796782/1240557)(即「使用配置文件」) – kryger

+0

/swagger-ui.html仍然可用,但沒有辦法。有沒有辦法禁止URL? – gstackoverflow

相關問題