Swagger 好早之前就更新到 3 ,不过一直没空和小伙伴们分享下具体玩法,主要是也是因为 Swagger 虽然升级了,但是我们在 Spring Boot 依然可以使用老版本的 Swagger,不过好像是从 Spring Boot2.6 开始,你会发现用不了老版本的 Swagger 了,哎,反正迟早都得搞,那不如就今天吧!
今天我们就来看看,在 Spring Boot2.7.1 中如何使用 Swagger3。
1. 依赖
首先我们创建一个 Spring Boot 项目,引入 Swagger3 依赖,如下:
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version></dependency>
以前在 Swagger2 那个时代,这个依赖我们需要引入两个,现在就只需要这一个即可。
2. 配置
接下来在启动类上添加两个注解,开启 Swagger:
@SpringBootApplication//开启swagger@EnableSwagger2@EnableOpenApi@EnableWebMvcpublic class SwaggerDemoApplication { public static void main(St