springboot2.7 整合swagger2
时间: 2023-10-30 13:06:48 浏览: 182
在Spring Boot 2.7中整合Swagger2的步骤如下:
1. 首先,将原来的swagger2和swagger-ui依赖删除,改为使用springfox-boot-starter依赖:
```xml
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
```
2. 添加Swagger2的依赖:
```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>
```
3. 在配置文件中添加如下配置:
```
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
```
相关问题
springboot2.6.1整合swagger2
Spring Boot 2.6.1可以通过添加Swagger2依赖来整合Swagger2。以下是整合步骤:
1. 在pom.xml文件中添加Swagger2依赖:
```
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3..</version>
</dependency>
```
2. 创建Swagger2配置类:
```
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo"))
.paths(PathSelectors.any())
.build();
}
}
```
3. 在Controller类上添加@Api注解:
```
@RestController
@RequestMapping("/api")
@Api(tags = "用户管理")
public class UserController {
// ...
}
```
4. 启动应用程序并访问http://localhost:808/swagger-ui.html,即可查看API文档。
以上就是Spring Boot 2.6.1整合Swagger2的简单步骤。
springboot3整合swagger2
在Spring Boot 3中整合Swagger 2可以通过以下步骤实现:
1. **添加依赖**:
首先,在你的`pom.xml`文件中添加Swagger 2的相关依赖:
```xml
<dependencies>
<!-- Swagger2 依赖 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
```
2. **配置Swagger**:
创建一个配置类来配置Swagger:
```java
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;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo"))
.paths(PathSelectors.any())
.build();
}
}
```
3. **启动应用并访问Swagger UI**:
启动你的Spring Boot应用,然后访问`http://localhost:8080/swagger-ui/`(默认端口是8080,具体端口根据你的应用配置可能有所不同)来查看Swagger UI。
4. **示例代码**:
下面是一个简单的示例,展示如何在Spring Boot 3中使用Swagger 2来生成API文档:
```java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
```
```java
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}
```
通过以上步骤,你就可以在Spring Boot 3中成功整合Swagger 2,并生成API文档。
阅读全文