springboot整合swagger2步骤
时间: 2023-04-05 21:02:28 浏览: 113
首先,在 pom.xml 文件中添加 swagger2 和 springfox-swagger2 依赖。然后,在 Spring Boot 启动类上添加 @EnableSwagger2 注解。接着,在配置类中添加 Docket Bean,配置 API 文档信息和扫描的包路径。最后,在浏览器中访问 http://localhost:8080/swagger-ui.html 即可查看 API 文档。
相关问题
springboot整合swagger2步骤demo
您好,以下是springboot整合swagger2的步骤demo:
1. 在pom.xml文件中添加swagger2的依赖:
```
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
```
2. 创建Swagger2配置类,用于配置Swagger2相关信息:
```
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot中使用Swagger2构建RESTful APIs")
.description("更多Spring Boot相关文章请关注:http://www.example.com/")
.termsOfServiceUrl("http://www.example.com/")
.contact("example")
.version("1.0")
.build();
}
}
```
3. 在Controller类中添加Swagger2注解,用于描述接口信息:
```
@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController {
@ApiOperation(value = "获取用户列表", notes = "获取所有用户列表")
@GetMapping("/list")
public List<User> list() {
// ...
}
@ApiOperation(value = "创建用户", notes = "根据User对象创建用户")
@ApiImplicitParam(name = "user", value = "用户实体", required = true, dataType = "User")
@PostMapping("/create")
public String create(@RequestBody User user) {
// ...
}
@ApiOperation(value = "更新用户", notes = "根据User对象更新用户")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long"),
@ApiImplicitParam(name = "user", value = "用户实体", required = true, dataType = "User")
})
@PutMapping("/update/{id}")
public String update(@PathVariable Long id, @RequestBody User user) {
// ...
}
@ApiOperation(value = "删除用户", notes = "根据ID删除用户")
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
@DeleteMapping("/delete/{id}")
public String delete(@PathVariable Long id) {
// ...
}
}
```
4. 启动应用程序,访问http://localhost:8080/swagger-ui.html,即可查看Swagger2生成的接口文档。
希望对您有所帮助!
springboot整合swagger2 3.0.0
要实现springboot整合swagger2 3.0.0版本,你需要按照以下步骤操作:
1. 创建一个maven项目并引入spring-boot-starter-web和springfox-boot-starter依赖。在pom.xml文件中添加以下代码:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- <version>2.5.6</version> -->
<!-- <version>2.6.3</version> -->
<!-- <version>2.6.5</version> -->
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
</dependency>
```
2. 在application.yml配置文件中添加以下内容:
```yaml
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
```
3. 创建启动类,并在其中添加`@EnableSwagger2`注解。例如:
```java
@SpringBootApplication
@EnableSwagger2
public class YourApplication {
public static void main(String[] args) {
SpringApplication.run(YourApplication.class, args);
}
}
```
这样就完成了springboot整合swagger2 3.0.0版本的配置。你可以根据需要在项目中编写相应的接口文档注解以及其他相关配置。如果需要更详细的操作步骤和示例代码,你可以参考中提供的链接。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [Springboot整合Swagger2(3.0.0版本)](https://blog.csdn.net/mo_sss/article/details/130820204)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *3* [Springboot整合Swagger UI 3.0.0 版本](https://blog.csdn.net/qq_42102911/article/details/126410050)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文