Swagger扫描多个路径

下一篇:swagger接口动态分组:Swagger接口动态分组_csdndemimang的博客-优快云博客前言:网上查的接口分组,都是手动配置多个Docket来实现分组,但是这样每加一个业务还要改这里代码,太麻烦所以自己改造了一下,代码效果如下注意:这种方式要保证接口要有一定的规则,比如(有如下三个接口1.aaa/xxx/xxx, 2.aaa/bbb/bbb,3.ccc/xx/bb)那么就会有aaa和ccc两个分组代码:import org.springframework.beans.factory.support.DefaultListableBeanFactory;import org.sphttps://blog.youkuaiyun.com/csdndemimang/article/details/121079081

主要是在于apis方法中使用Predicates.or(path1,path2)的方法

扫描单个

return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                // 自行修改为自己的包路径
                .apis(Swagger2Config.basePackage("com.dnkj.smart.controller"))
                .paths(PathSelectors.any())
                .build()
        		.globalOperationParameters(pars);//添加请求头参数

扫描多个

return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                // 自行修改为自己的包路径
                .apis(Predicates.or(Swagger2Config.basePackage("com.dnkj.smart.controller"), Swagger2Config.basePackage("com.dnkj.digital.controller")))
                .paths(PathSelectors.any())
                .build()
        		.globalOperationParameters(pars);//添加请求头参数

Swagger 配置类可以指定多个扫描路径。在 Spring 中使用 Swagger 时,通过`RequestHandlerSelectors.basePackage`方法来指定扫描的包路径,若要指定多个扫描路径,可以使用`RequestHandlerSelectors.or`方法将多个路径组合起来。 以下是一个示例代码: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket creatResetApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.or( RequestHandlerSelectors.basePackage("com.example.controller"), RequestHandlerSelectors.basePackage("com.example.service") )) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("swagger 构建接口文档") .description("测试用例") .termsOfServiceUrl("http://www.baidu.com") .version("1.0") .build(); } } ``` 在上述代码中,`RequestHandlerSelectors.or`方法将`com.example.controller`和`com.example.service`两个包路径组合在一起,这样 Swagger 就会同时扫描这两个包下的类。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值