springboot3整合swagger
Spring Boot 3整合Swagger的步骤如下:
- 在pom.xml中添加Swagger依赖:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
- 创建Swagger配置类:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.OAS_30)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
- 在Spring Boot应用的主类或者配置类上添加
@EnableSwagger2
注解启用Swagger。 - 运行Spring Boot应用,访问
http://<host>:<port>/swagger-ui/index.html
查看Swagger文档。
以下是一个简单的Spring Boot 3应用整合Swagger的示例:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.oas.annotations.EnableOpenApi;
@SpringBootApplication
@EnableOpenApi
public class SwaggerDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SwaggerDemoApplication.class, args);
}
}
确保你的Spring Boot版本与Swagger版本兼容。Spring Boot 3需要使用Swagger 3。
评论已关闭