springboot整合swagger-ui
时间: 2023-10-08 16:13:17 浏览: 155
要在Spring Boot项目中整合Swagger UI,可以按照以下步骤进行操作:
1. 在项目的Maven或Gradle配置中添加Swagger依赖,例如:
```xml
<!-- Maven -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
```
```groovy
// Gradle
implementation 'io.springfox:springfox-boot-starter:3.0.0'
```
2. 创建一个配置类(例如`SwaggerConfig`)并添加`@Configuration`和`@EnableSwagger2`注解,示例如下:
```java
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
// 配置内容
}
```
3. 在配置类中,可以使用`Docket`来配置Swagger的基本信息,例如设置API的标题、描述等,示例如下:
```java
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import org.springframework.context.annotation.Bean;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.controllers"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("API Documentation")
.description("API Documentation for my project")
.version("1.0.0")
.build();
}
}
```
4. 启动项目,访问`http://localhost:8080/swagger-ui.html`即可查看Swagger UI界面。
阅读全文