springboot 2.3.12.RELEASE集成swagger
时间: 2023-12-10 20:35:09 浏览: 226
以下是在Spring Boot 2.3.12.RELEASE中集成Swagger的步骤:
1.在pom.xml文件中添加Swagger依赖:
```xml
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
```
2.创建Swagger配置类,用于配置Swagger:
```java
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
```
3.在Controller类中添加Swagger注解,用于描述API接口:
```java
@RestController
@RequestMapping("/api")
@Api(value = "API", tags = "API接口")
public class ApiController {
@ApiOperation(value = "获取用户信息", notes = "根据用户ID获取用户信息")
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "int", paramType = "path")
@GetMapping("/user/{id}")
public User getUserById(@PathVariable int id) {
// 根据ID获取用户信息
}
}
```
4.启动Spring Boot应用程序,访问http://localhost:8080/swagger-ui.html,即可查看Swagger UI界面,其中包含了所有API接口的详细信息。
阅读全文