swagger3和spingboot兼容
时间: 2024-01-17 21:14:36 浏览: 87
Swagger 3 和 Spring Boot 是兼容的,可以一起使用。Spring Boot 提供了一个集成 Swagger 的简单方式,只需要添加相关的依赖和配置即可。在 Spring Boot 中,可以使用 springfox-swagger2 或 springfox-swagger-ui 依赖来集成 Swagger 2;或者使用 springfox-swagger3 或 springfox-swagger-ui 依赖来集成 Swagger 3。
下面是一个使用 springfox-swagger3 的 Spring Boot 配置示例:
1. 添加以下依赖:
```
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger3</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
```
2. 在 Spring Boot 应用程序的主类上添加 @EnableSwagger2WebMvc 注解:
```
@SpringBootApplication
@EnableSwagger2WebMvc
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
```
3. 添加 Swagger 配置类:
```
@Configuration
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.controller"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("My API")
.description("My API description")
.version("1.0.0")
.build();
}
}
```
4. 运行应用程序,并访问 http://localhost:8080/swagger-ui/index.html,即可查看 API 文档。
阅读全文
相关推荐


















