How do you turn off swagger-ui in production

前端 未结 6 1612
孤独总比滥情好
孤独总比滥情好 2021-01-31 14:46

I have swagger plugged in to my spring boot application. Spring boot allows you to have property files for each environment that you have. Is there a way to disable swagger fo

相关标签:
6条回答
  • 2021-01-31 14:57

    If you are working on multiple environments then you can also use @Profile as array

    @Configuration
    @EnableSwagger2
    @Profile({"dev","qa"})
    public class SwaggerConfig {
       // your swagger configuration
    }
    
    0 讨论(0)
  • For those that use code gen (which generates Swagger2SpringBoot):

    1. Write your own Swagger2SpringBoot (with the @Profile bit) and locate it in the same package path as the autogenerated one.
    2. Edit swagger-codegen-maven-plugin to place generated into src/main/java (which will overwrite your own one in point 1.
    3. Edit .swagger-codegen-ignore to not overwrite your Swagger2SpringBoot
    4. Note other stuff will also be overwritten eg. pom.xml and application.properties. Just add them to .swagger-codegen-ignore too.

    Done.

    0 讨论(0)
  • 2021-01-31 15:06

    This is my configuration class:

    @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", "dev@mailbox"))
                    .build();
        }
    }
    

    Wherever I need Swagger, I add the profile swagger to the environment variable SPRING_PROFILES_ACTIVE

    0 讨论(0)
  • 2021-01-31 15:07
    1. have configuration for env

      @Configuration

      @EnableSwagger2

      @Profile("devone")

    2. application.yaml

      profiles: 
      
      active:
      
       ${MY_ENV:devone}
      

    MY_ENV you will read from file, like .env

    .env file content: MY_ENV=prod

    In the production keep other .env file only for production credentials.

    0 讨论(0)
  • 2021-01-31 15:07

    spring.profiles.active=production with @Profile("!production") worked for me to turn off swagger in prod.

    Ex :-

    @Profile("!production")
    @Component
    @EnableSwagger2
    public class SwaggerConfig {
           //TODO
    }
    
    0 讨论(0)
  • 2021-01-31 15:22

    Put your swagger configuration into separate configuration class and annotate it with @Profile annotation -> so that it will be scanned into Spring context only in certain profiles.

    Example:

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

    You can than define profile your Spring Boot app is operating in via command line: --spring.profiles.active=dev or via config file: spring.profiles.active=dev.

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

    0 讨论(0)
提交回复
热议问题