本文主要是介绍如何swagger关闭及给swagger加参数信息,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
项目的swagger方便研发人员调试,上线之后需要将swagger关闭这时候需要给原来的@Configuration注解换成@ConditionlOnProperty注解及可。注解参数信息
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
若swagger想加入额外参数类型,代码如下
首先是正常的swagger配置正常界面及调试方法界面
import org.springframework.boot.autoconfigure.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.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
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;/******************************* 用途说明: Swagger配置******************************/
@EnableSwagger2
//@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
@Configuration
public class SwaggerConfig {//生成swagger文档相关配置@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.test"))//扫描com路径下的api文档.paths(PathSelectors.any())//路径判断.build();}//用途说明: 生成swagger文档相关配置private ApiInfo apiInfo() {return new ApiInfoBuilder().title("swagger服务接口测试平台").contact(new Contact("dexi.chi攻城狮", "https://test", "")).version("1.0.0.RELEASE").build();}
}
改造后代码
import org.springframework.boot.autoconfigure.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.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
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;/******************************* 用途说明: Swagger配置******************************/
@EnableSwagger2
//@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
@Configuration
public class SwaggerConfig {//生成swagger文档相关配置@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.test"))//扫描com路径下的api文档.paths(PathSelectors.any())//路径判断.build().globalOperationParameters(getParameterList());}//用途说明: 生成swagger文档相关配置private ApiInfo apiInfo() {return new ApiInfoBuilder().title("swagger服务接口测试平台").contact(new Contact("dexi.chi攻城狮", "https://test", "")).version("1.0.0.RELEASE").build();}private List<Parameter> getParameterList() {ParameterBuilder parameterBuilder = new ParameterBuilder();List<Parameter> pars = new ArrayList<>();pars.add(parameterBuilder.name("userNo").description("校验人员工号").modelRef(new ModelRef("string")).parameterType("header").required(true).build());return pars;}
}
这篇关于如何swagger关闭及给swagger加参数信息的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!