1.Swagger 是什么?
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<scope>provided </scope>
</dependency>
2.整合Swagger2
2.1 新增二级父模块common 并修改pom文件
注意:设置为父模块 pom
<artifactId>common</artifactId>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>provided </scope>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<scope>provided </scope>
</dependency>
<!--lombok用来简化实体类:需要安装lombok插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided </scope>
</dependency>
<!--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>
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- spring2.X集成redis所需common-pool2
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.0</version>
</dependency>-->
</dependencies>
2.2 创建三级模块service_base
(1)创建
(2)添加配置类
import com.google.common.base.Predicates;
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.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2 // 开启 Swagger2
public class SwaggerConfig {
@Bean
public Docket webApiConfig(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("webApi")
.apiInfo(webApiInfo())
.select()
// 当Url里有/admin/.** 相关的接口不暴露
.paths(Predicates.not(PathSelectors.regex("/admin/.*")))// 不去暴露admin相关的信息
// 当Url里有/error/.** 相关的接口不暴露
.paths(Predicates.not(PathSelectors.regex("/error.*")))// 不去暴露相关的error信息
.build();
}
/**
定义文档描述
*/
private ApiInfo webApiInfo(){
return new ApiInfoBuilder()
// 文档标题
.title("网站-课程中心API文档")
// 文档描述
.description("本文档描述了课程中心微服务接口定义")
// 版本信息(自己定义,无需改)
.version("1.0")
// 作者 总的url 邮箱
.contact(new Contact("Helen", "http://atguigu.com", "55317332@qq.com"))
.build();
}
}
2.3 在需要使用swagger的服务模块引入依赖
在service模块下引入依赖
<dependency>
<groupId>com.atguigu</groupId>
<artifactId>service_base</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
2.4 在服务模块启动类添加扫描注解(指定扫描swagger的包路径)
@SpringBootApplication
@ComponentScan(basePackages = {"com.atguigu"})
public class EduApplication {
public static void main(String[] args) {
SpringApplication.run(EduApplication.class,args);
}
}
2.5 测试下
访问:http://localhost:8001/swagger-ui.html
2.6 优化下,添加接口描述
优化效果如下: