When using spring-boot development, we often use swagger as API document output. You can see the path, parameters, etc. of the API on the UI interface.
Of course, it is very convenient as a development environment, but when using a production environment, we need to ban swagger. How to disable swagger through configuration file method?
The code is as follows:
import org.springframework.boot.autoconfigure.condition.Condition.ConditionalOnProperty;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import springfox.documentation.builders.ApiInfoBuilder;import springfox.documentation.builders.ParameterBuilder;import springfox.documentation.builders.RequestHandlerSelectors;import springfox.documentation.schema.ModelRef;import springfox.documentation.service.Parameter;import springfox.documentation.spi.DocumentationType;import springfox.documentation.spring.web.plugins.Docket;import springfox.documentation.swagger2.annotations.EnableSwagger2;import java.util.ArrayList;import java.util.List;/** * Created by Bane.Shi. * User: Bane.Shi * Date: 2017/12/28 * Time: 2:15 pm */@Configuration@ConditionalOnProperty(prefix = "swagger",value = {"enable"},havingValue = "true")@EnableSwagger2public class SwaggerConfiguration { @Bean public Docket swagger(){ return new Docket(DocumentationType.SWAGGER_2) .groupName("default") .apiInfo(new ApiInfoBuilder().title("SSP School API").version("1.0.0").build()) .select() .apis(RequestHandlerSelectors.basePackage("com.fclassroom.ssp.school")) .build() .globalOperationParameters(globalOperationParameters()); } private List<Parameter> globalOperationParameters(){ List<Parameter> parameters = new ArrayList<>(); // parameters.add(new ParameterBuilder().name("ACCESS-TOKEN").description("ACCESS-TOKEN").required(false).parameterType("header").modelRef(new ModelRef("string")).build()); return parameters; }}If you want to enable swagger, add it to the configuration file
swagger.enable=true
The key is @ConditionalOnProperty here
The attribute key here is swagger.enable , havingValue is the expected value, which will only take effect when the value is equal to the expected value. In other words, swagger.enable will only take effect when true, and other values or no set values will not take effect.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.