1、认识Swagger
swagger是一个规范完整的框架,用于生成、描述、调用和可视化的Restful风格的web服务,总体目标是使用客户端的文件系统作为服务器以同样的速度来更新,文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步
作用:
1. 接口的文档在线自动生成。
2. 功能测试。
swagge是一组开源项目,其主要项目如下
-
Swagger-tools:提供各种与Swagger进行集成和交互的工具。例如模式检验、Swagger 1.2文档转换成Swagger 2.0文档等功能。
-
Swagger-core: 用于Java/Scala的的Swagger实现。与JAX-RS(Jersey、Resteasy、CXF…)、Servlets和Play框架进行集成。
-
Swagger-js: 用于JavaScript的Swagger实现。
-
Swagger-node-express: Swagger模块,用于node.js的Express web应用框架。
-
Swagger-ui:一个无依赖的HTML、JS和CSS集合,可以为Swagger兼容API动态生成优雅文档。
-
Swagger-codegen:一个模板驱动引擎,通过分析用户Swagger资源声明以各种语言生成客户端代码。
2、MAVEN
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
3、配置swagger2配置类
package com.atlziy.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
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;
import java.util.ArrayList;
/**
* 要求 再生产环境中适用swagger 在正式环境中不使用
*
* @author li
* @Created by li on 2020/9/3 22:27.
*/
@Configuration
@EnableSwagger2//开启swagger2
public class SwaggerConfig {
//配置swagger的docket的bean实例
@Bean
public Docket docket(Environment environment) {
//设置要显示的swagger的生产环境:
Profiles profiles = Profiles.of("dev","test");
//判断是否处在当前设定的环境中
boolean b = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2).groupName("洋哥")
.apiInfo(apiInfo()).enable(b)
.select()
//RequestHandleerSelectors,配置要扫描接口的方式
//basePackage:指定需要扫描的包
//any():扫描全部
//none():不扫描
//withClassAnnotation:扫描类上的注解,参数是一个注解的反射对象
//withMethodAnnotation:扫描方法上的注解
.apis(RequestHandlerSelectors
.basePackage("com.atlziy.controller"))
//path()过滤什么路径
//.paths(PathSelectors.ant("/kuang/**"))
.build();
}
//我们可以配置多个协同使用,在开发环境中 不同人员使用自己的bean 进行配置 就可以获取自己的swagger页面
@Bean
public Docket docket1(Environment environment) {
return new Docket(DocumentationType.SWAGGER_2).groupName("A");
}
@Bean
public Docket docket2(Environment environment) {
return new Docket(DocumentationType.SWAGGER_2).groupName("B");
}
@Bean
public Docket docket3(Environment environment) {
return new Docket(DocumentationType.SWAGGER_2).groupName("C");
}
//配置swagger的信息
public ApiInfo apiInfo() {
Contact contact = new Contact("洋哥", "", "2470334493@qq.com");
return new ApiInfo("洋哥的api文档",
"靠自己赢得一切",
"1.0",
"urn:tos",
contact,//作者信息
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
显示页面:
测试方式:
显示测试结果