1. 导入相关maven依赖
<!--swagger2-->
<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>
2. 在项目根目录,即启动类Application.java
同级目录创建配置文件Swagger2的配置文件SwaggerConfig.java
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
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.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;
/**
* @description: Swagger2的配置文件,在项目的启动类的同级文件建立
* @author: zyb
* @date: 2021/5/14 11:18
*/
@Configuration
@EnableSwagger2
//是否开启swagger,正式环境一般是需要关闭的(避免不必要的漏洞暴露!),可根据springboot的多环境配置进行设置
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
public class SwaggerConfig {
/**
* swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
// 为当前包路径
.apis(RequestHandlerSelectors.basePackage("cn.graduation.bbs.controller")).paths(PathSelectors.any())
.build();
}
/**
* 构建 api文档的详细信息函数,注意这里的注解引用的是哪个
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
// 页面标题
.title("Silence社区 Swagger2 构建 RestFul API")
// 创建人信息
.contact(new Contact("zyb", "https://blog.csdn.net/qq_40406380?spm=1000.2115.3001.5343", "1520949225@qq.com"))
// 版本号
.version("1.0")
// 描述
.description("API 描述")
.build();
}
}
注:可在application.properties
或者application.yml
文件中配置是否开启
#application.properties文件,是否激活 swagger true or false
swagger.enable=true
#application.yml文件,是否激活 swagger true or false
swagger:
enable: true
3. 启动项目之后访问ip:端口号/swagger-ui.html
即可看到Swagger2的ui界面
4. 常用注解:https://blog.csdn.net/xiaojin21cen/article/details/78654652
5. 使用fastmock(和easymock类似)模拟数据
fastmock的github地址:https://github.com/Marvengong/fastmock
fastmock官网:https://www.fastmock.site/#/
-
注册账号登录即可创建项目以及接口模拟数据
网友评论