官方Starter
依赖
首先在
pom.xml中添加springfox官方Swagger依赖;
<!--springfox swagger官方Starter-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
添加Swagger的Java配置,配置好Api信息和需要生成接口文档的类扫描路径即可;
/**
* Swagger2 API文档配置类
*
* 作用:配置Swagger2自动生成RESTful API文档
* 访问地址:http://localhost:端口/swagger-ui.html
*/
@Configuration // 声明为Spring配置类
public class Swagger2Config {
/**
* 创建Swagger核心配置Docket
*
* @return Docket对象,包含所有Swagger配置
*/
@Profile({"dev", "test"}) // 只在开发和测试环境启用
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2) // 指定使用Swagger2规范
.apiInfo(apiInfo()) // 设置文档基本信息
.select() // 开始配置接口扫描规则
// 指定要扫描的Controller包路径
.apis(RequestHandlerSelectors.basePackage("com.macro.mall.tiny.controller"))
// 包含所有路径
.paths(PathSelectors.any())
.build(); // 构建Docket对象
}
/**
* 配置API文档的基本信息
*
* @return ApiInfo对象,包含文档元数据
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("SwaggerUI演示") // 文档标题
.description("mall-tiny") // 项目描述
// 联系人信息(名称,网址,邮箱)
.contact(new Contact("macro", null, null))
.version("1.0") // API版本号
.build(); // 构建ApiInfo对象
}
}
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 jungle8884@163.com