Spring Boot 使用 Swagger3 生成 API 接口文档

本文介绍了如何在Spring Boot项目中使用Swagger3生成API接口文档。通过配置@EnableOpenApi注解,结合Docket Bean来自定义配置,实现接口的文档化。Swagger3与Swagger2的主要区别在于启用的属性不同,如使用DocumentationType.OAS_30。文章还展示了如何编写实体类和接口,以及如何通过访问Swagger UI来查看和测试接口。
摘要由CSDN通过智能技术生成

import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;

import springfox.documentation.builders.PathSelectors;

import springfox.documentation.builders.RequestHandlerSelectors;

import springfox.documentation.oas.annotations.EnableOpenApi;

import springfox.documentation.service.ApiInfo;

import springfox.documentation.service.Contact;

import springfox.documentation.spi.DocumentationType;

import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;

/**

  • Created with IntelliJ IDEA.

  • @author : 村雨遥

  • @version : 1.0

  • @project : springboot-swagger3-demo

  • @package : com.cunyu.springbootswagger3demo.config

  • @className : SwaggerConfig

  • @createTime : 2022/1/6 14:19

  • @email : 747731461@qq.com

  • @微信 : cunyu1024

  • @公众号 : 村雨遥

  • @网站 : https://cunyu1943.github.io

  • @description :

*/

@Configuration

@EnableOpenApi

public class SwaggerConfig {

/**

  • 用于读取配置文件 application.properties 中 swagger 属性是否开启

*/

@Value(“${swagger.enabled}”)

Boolean swaggerEnabled;

@Bean

public Docket docket() {

return new Docket(DocumentationType.OAS_30)

.apiInfo(apiInfo())

// 是否开启swagger

.enable(swaggerEnabled)

.select()

// 过滤条件,扫描指定路径下的文件

.apis(RequestHandlerSelectors.basePackage(“com.cunyu.springbootswagger3demo.controller”))

// 指定路径处理,PathSelectors.any()代表不过滤任何路径

//.paths(PathSelectors.any())

.build();

}

private ApiInfo apiInfo() {

/作者信息/

Contact contact = new Contact(“村雨遥”, “https://cunyu1943.github.io”, “747731461@qq.com”);

return new ApiInfo(

“Spring Boot 集成 Swagger3 测试”,

“Spring Boot 集成 Swagger3 测试接口文档”,

“v1.0”,

“https://cunyu1943.github.io”,

contact,

“Apache 2.0”,

“http

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值