spring boot 1.5.9 集成swagger
时间: 2023-09-02 19:07:18 浏览: 312
要在Spring Boot 1.5.9中集成Swagger,需要进行以下步骤:
1. 通过Maven或Gradle将Swagger添加到项目依赖中:
```
<!-- Swagger -->
<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的配置类:
```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的注解:
```java
@RestController
@RequestMapping("/api")
@Api(value = "API", produces = MediaType.APPLICATION_JSON_VALUE)
public class ApiController {
@ApiOperation(value = "Get all users", response = User.class, responseContainer = "List")
@GetMapping("/users")
public List<User> getAllUsers() {
// ...
}
// ...
}
```
4. 运行应用程序并访问`http://localhost:8080/swagger-ui.html`来查看Swagger UI。
注意:Spring Boot 1.5.x版本中的Swagger版本需要与上述示例中使用的版本相对应。如果您使用不同的版本,则可能需要相应地更改配置和依赖项。
阅读全文
相关推荐













