方法一(最普通的方式):匹配一个controller目录下的所有controller类。

  

 1  @Bean
2 public Docket creatRestApi(){
3 return new Docket(DocumentationType.SWAGGER_2)
4 .apiInfo(apiInfo())
5 .select()
6 //关键语句
7 .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")
8 )
9 .paths(PathSelectors.any())
10 .build();
11 }

方法二:匹配有指定注解的类。

 1  @Bean
2 public Docket creatRestApi(){
3 return new Docket(DocumentationType.SWAGGER_2)
4 .apiInfo(apiInfo())
5 .select()
6 //关键语句
7 .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)
8 )
9 .paths(PathSelectors.any())
10 .build();
11 }

方法三:匹配有指定注解的方法。

 1 @Bean
2 public Docket creatRestApi(){
3 return new Docket(DocumentationType.SWAGGER_2)
4 .apiInfo(apiInfo())
5 .select()
6 //关键语句
7 .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)
8 )
9 .paths(PathSelectors.any())
10 .build();
11 }

方法四:我觉得是最重要的方法:可以匹配多个controller包(多个controller包不在同一级)

 1  @Bean
2 public Docket creatRestApi(){
3 return new Docket(DocumentationType.SWAGGER_2)
4 .apiInfo(apiInfo())
5 .select()
6 .apis(Predicates.or(RequestHandlerSelectors.basePackage("com.example.demo.controller"),
7 RequestHandlerSelectors.basePackage("com.example.demo.gp.controller"),
8 RequestHandlerSelectors.basePackage("com.gp.callback.controller"))
9 )
10 .paths(PathSelectors.any())
11 .build();
12
13 }

如果上面的Predicates类报红的话,pom文件导入jar包

 <!-- guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.0</version>
</dependency>

总结:相比四种方法,如果项目中只有一个controller包的话,直接使用方法一就可以了,如果项目中有多个controller包,那么我建议使用方法四。

最新文章

  1. BZOJ 3110: [Zjoi2013]K大数查询 [树套树]
  2. 软件工程(FZU2015)赛季得分榜,第10回合(alpha冲刺)
  3. javascript 字符串加密的几种方法
  4. Chrome浏览器扩展开发系列之十四
  5. Kafka学习笔记(一):概念介绍
  6. 转:The Knuth-Morris-Pratt Algorithm in my own words
  7. _BLOCK_TYPE_IS_VALID 问题解析及处理
  8. Apache 相关配置
  9. Hbase压力测试
  10. Linux下gdb的安装及使用入门
  11. xen虚拟机(Centos6.6)的创建和扩容
  12. RTSP连接中断重连的问题
  13. go 数组 切片 字典 结构体
  14. java利用jxl实现Excel导入功能
  15. 如何解决Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 8000401a. 问题
  16. Saiku权限控制(四)
  17. (android高仿系列)今日头条 --新闻阅读器 (转载)
  18. 深入了解Java虚拟机(3-1)虚拟机类加载机制
  19. CLR基础
  20. Python中scatter()函数--转载

热门文章

  1. 数值分析:矩阵奇异值分解(Numpy实现)
  2. 浅谈Java和JavaScript中变量和数据类型的区别
  3. BZOJ 3729 - Gty的游戏(Staircase 博弈+时间轴分块)
  4. 洛谷 P7156 - [USACO20DEC] Cowmistry P(分类讨论+trie 树上 dp)
  5. 文件/目录对比:diff命令
  6. 解决Gitlab的The remote end hung up unexpectedly错误,解决RPC failed; HTTP 413 curl 22 The requested URL returned error: 413 Request Entity Too Large问题
  7. 【Python小试】去除核酸特定长度的接头序列
  8. LearnPython_week4
  9. 一站式Flink&amp;Spark平台解决方案——StreamX
  10. 移动开发之h5学习大纲