十大中文网站排名,互联网平台公司有哪些,教育类网站设计,南宫建设局网站首页Swagger配置扫描接口
1、构建Docket时通过select()方法配置怎么扫描接口。
Bean
public Docket docket() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()// 通过.select()方法#xff0c;去配置扫描接口,RequestHandlerSelectors配置如何扫描…Swagger配置扫描接口
1、构建Docket时通过select()方法配置怎么扫描接口。
Bean
public Docket docket() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()// 通过.select()方法去配置扫描接口,RequestHandlerSelectors配置如何扫描接口.apis(RequestHandlerSelectors.basePackage(com.kuang.swagger.controller)).build();
}2、重启项目测试由于我们配置根据包的路径扫描接口所以我们只能看到一个类
3、除了通过包路径配置扫描接口外还可以通过配置其他方式扫描接口这里注释一下所有的配置方式
any() // 扫描所有项目中的所有接口都会被扫描到
none() // 不扫描接口
// 通过方法上的注解扫描如withMethodAnnotation(GetMapping.class)只扫描get请求
withMethodAnnotation(final Class? extends Annotation annotation)
// 通过类上的注解扫描如.withClassAnnotation(Controller.class)只扫描有controller注解的类中的接口
withClassAnnotation(final Class? extends Annotation annotation)
basePackage(final String basePackage) // 根据包路径扫描接口4、除此之外我们还可以配置接口扫描过滤
Bean
public Docket docket() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()// 通过.select()方法去配置扫描接口,RequestHandlerSelectors配置如何扫描接口.apis(RequestHandlerSelectors.basePackage(com.kuang.swagger.controller))// 配置如何通过path过滤,即这里只扫描请求以/kuang开头的接口.paths(PathSelectors.ant(/kuang/**)).build();
}5、这里的可选值还有
any() // 任何请求都扫描
none() // 任何请求都不扫描
regex(final String pathRegex) // 通过正则表达式控制
ant(final String antPattern) // 通过ant()控制配置Swagger开关
1、通过enable()方法配置是否启用swagger如果是falseswagger将不能在浏览器中访问了
Bean
public Docket docket() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).enable(false) //配置是否启用Swagger如果是false在浏览器将无法访问.select()// 通过.select()方法去配置扫描接口,RequestHandlerSelectors配置如何扫描接口.apis(RequestHandlerSelectors.basePackage(com.kuang.swagger.controller))// 配置如何通过path过滤,即这里只扫描请求以/kuang开头的接口.paths(PathSelectors.ant(/kuang/**)).build();
}2、如何动态配置当项目处于test、dev环境时显示swagger处于prod时不显示
Bean
public Docket docket(Environment environment) {// 设置要显示swagger的环境Profiles of Profiles.of(dev, test);// 判断当前是否处于该环境// 通过 enable() 接收此参数判断是否要显示boolean b environment.acceptsProfiles(of);return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).enable(b) //配置是否启用Swagger如果是false在浏览器将无法访问.select()// 通过.select()方法去配置扫描接口,RequestHandlerSelectors配置如何扫描接口.apis(RequestHandlerSelectors.basePackage(com.kuang.swagger.controller))// 配置如何通过path过滤,即这里只扫描请求以/kuang开头的接口.paths(PathSelectors.ant(/kuang/**)).build();
}3、可以在项目中增加一个dev的配置文件查看效果 总代码
SwaggerConfig
package com.xxxx.swagger2.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;import java.util.ArrayList;import static springfox.documentation.service.ApiInfo.DEFAULT_CONTACT;Configuration
// 开启Swagger2
EnableSwagger2
public class SwaggerConfig {// 配置了Swagger的Docket的bean实例// enable是否启动Swagger,如果为false,则Swagger不能在浏览器中访问Beanpublic Docket docket(Environment environment){// 设置要显示的Swagger环境Profiles profiles Profiles.of(dev,test);// 通过environment.acceptsProfiles判断是否处在自己设定的环境当中boolean flag environment.acceptsProfiles(profiles);return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).enable(flag).select()// RequestHandlerSelectors ,配置要扫描接口的方式// basePackage:指定要扫描的包// any():扫描全部// none():都不扫描// withClassAnnotation:扫描类上的注解,参数是一个注解的反射对象// withMethodAnnotation:扫描方法上的注解.apis(RequestHandlerSelectors.basePackage(com.xxxx.swagger2.controller))// paths():过滤什么路径// .paths(PathSelectors.ant(/xxxx/**)).build();}// 配置Swagger信息 apiInfo()private ApiInfo apiInfo(){Contact contact new Contact(firetang,http://wwww.baidu.com,123132qq.com);return new ApiInfo(firetangApidocument,the author is very cool,1.0,http://www.baidu.com,contact,Apache 2.0,http://www.apache.org/licenses/LICENSE-2.0,new ArrayListVendorExtension());}}
application.properties spring.profiles.activedevapplication-dev.properties
server.port8081application-pro.properties
server.port8082