方式一:
如果你的项目工程目录结构是下面这样的:
则使用多个controller的共同拥有的“父类”,即精确到这些个controller的上一级目录。按照以下方式进行扫描包:
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.tfjybj.itoo.controller"))
.paths(PathSelectors.any())
.build();
}
方式二:
如果你的项目工程目录结构是下面这样的:
这些controller拥有不同的上级目录,就不能再使用方式一中的方式扫描controller所在“父类”的包了,用下面的方式进行扫描controllerl类:指定所有controller都实现的一个接口,比如@RestController(Controller类上使用的注解)
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
.paths(PathSelectors.any())
.build();
}
扫描带@RestController注解的controller类:这样就可以读取到不在同一个包下的controller类了。
错误配置方式:
下面两种扫描多个不在同一包下的controller类的方式,都是不正确的。都会出现这个问题:No operations defined in spec!
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.glodon.kgb.system.*.controller"))
.paths(PathSelectors.any())
.build();
}
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.glodon.kgb.system.data.controller"))
.apis(RequestHandlerSelectors.basePackage("com.glodon.kgb.system.model.controller"))
.paths(PathSelectors.any())
.build();
}