在guli-parent下创建common模块(maven工程)
在common中引入相关依赖
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<scope>provided </scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<scope>provided </scope>
</dependency>
类型改为pom,因为它下面还有子模块
<artifactId>common</artifactId>
<packaging>pom</packaging>
在common下面创建子模块service-base
在模块service-base中,创建包com.atguigu.servicebase.config,创建配置类SwaggerConfig
@Configuration // 配置类
@EnableSwagger2 // swagger注解,表示做的是swagger整合
public class SwaggerConfig {
@Bean
public Docket webApiConfig(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("webApi")
.apiInfo(webApiInfo())
.select()
.paths(Predicates.not(PathSelectors.regex("/admin/.*")))
.paths(Predicates.not(PathSelectors.regex("/error.*")))
.build();
}
private ApiInfo webApiInfo(){
return new ApiInfoBuilder()
.title("网站-课程中心API文档")
.description("本文档描述了课程中心微服务接口定义")
.version("1.0")
.contact(new Contact("java", "http://atguigu.com", "1123@qq.com"))
.build();
}
}
在service-edu中引入service-base
现在的swagger是在common里面,并不是在service-edu中,service-edu中想要用common里的东西,需要在service-edu中引入service-base,为了方便,统一放到service模块中,在pom里引入依赖
<dependency>
<groupId>com.atguigu</groupId>
<artifactId>service-base</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
在启动类里添加注解@ComponentScan
spring boot默认从外往里扫,因此无法扫描到配置类SwaggerConfig,需要自己设置包扫描规则
@ComponentScan(basePackages = {"com.atguigu"}) // 设置包扫描规则,到包com.atguigu里进行扫描
定义在类上
@Api(description="讲师管理")
定义在方法上
@ApiOperation(value = "根据ID删除讲师")
定义在参数上
@ApiParam(name = "id", value = "讲师ID", required = true)