SpringBoot(三)整合Swagger3框架

基于:【狂神说Java】SpringBoot最新教程IDEA版通俗易懂

1 前后端分离

  • 早些年,后端时代,前端写html、css静态页面交给后端,后端根据模板引擎改数据,跳转逻辑数据渲染,MVC模式;
  • 前后端分离:后端控制层、服务层、数据访问层;前端控制层、视图层;交互靠API接口传JSON

2 swagger简介

  • Restful API文档在线自动生成,API文档与API定义同步更新;
  • 直接运行,可以在线测试API接口;

3 使用

3.1 导入依赖

<!--Swagger-->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

3.2 在启动主类注解@EnableOpenApi

@SpringBootApplication
@EnableOpenApi
public class DeepmezProApplication {
	public static void main(String[] args) {
		SpringApplication.run(DeepmezProApplication.class, args);
	}
}

3.3 配置Swagger3Config

@Configuration
public class Swagger3Config {
    @Bean
    public Docket createRestApi() {
        // 建造者模式构建Docket
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .groupName("DeepMez")
                .select()
                // 需要放出的接口
                .apis(RequestHandlerSelectors.basePackage("xyz.deepmez.controller"))
                // 过滤
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("接口文档")
                .description("描述")
                .contact(new Contact("root", "demo.com", "easy@163.com"))
                .version("1.0")
                .build();
    }
}

可以在/config下面配置Swagger3Config.java

3.4 访问使用

http://localhost:8080/swagger-ui/index.html

4 总结

前后端分离模式的软件设计是通过数据接口来交互,所以对于接口的设计就较为重要了。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
SpringBoot 3集成Swagger 3的步骤如下: 1. 首先,需要在项目的pom.xml文件中添加SpringFox的依赖项。可以使用以下代码片段添加依赖项: ``` <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> ``` 这将帮助将Swagger 3集成到Spring中。 2. 在控制器类上添加@Api注解,用于定义Swagger页面的信息。例如: ``` @RestController @RequestMapping("/user") @Api(tags = "人员信息查询接口") public class UserController { // ... } ``` 这将在Swagger页面上显示"人员信息查询接口"标签。 3. 在需要生成API文档的方法上添加@ApiOperation注解,用于定义方法的说明。例如: ``` @GetMapping("/queryPage") @ApiOperation(value = "人员信息分页查询方法", notes = "查看人员信息是否返回成功") public Result queryPage(Integer pageNumber, Integer pageSize, String name) { // ... } ``` 这将在Swagger页面上显示"人员信息分页查询方法"的说明。 4. 最后,根据需要进行其他配置。例如,如果需要修改SpringBoot的路径匹配模式,可以在application.yml文件中进行配置。例如: ``` spring: mvc: pathmatch: matching-strategy: ANT_PATH_MATCHER ``` 这将使用AntPathMatcher作为路径匹配策略。 综上所述,以上是SpringBoot 3集成Swagger 3的基本步骤和配置。通过这些步骤,您可以在SpringBoot项目中使用Swagger来生成API文档。 #### 引用[.reference_title] - *1* *3* [SpringBoot整合Swagger3.0](https://blog.csdn.net/weixin_43709291/article/details/127192273)[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^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Springboot项目集成Swagger3.0](https://blog.csdn.net/weixin_44924882/article/details/127940544)[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^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值