SpringBoot集成Swagger

SpringBoot集成Swagger以及常用注解

一、什么是Swagger

官方描述:

原文:

Simplify API development for users, teams, and enterprises with the Swagger open source and professional toolset. Find out how Swagger can help you design and document your APIs at scale.

百度翻译:

使用Stragger开源和专业工具集简化用户、团队和企业的API开发。了解Swagger如何帮助您大规模地设计和记录API。

个人理解:

Swagger说白了就是一个便于前后端开发人员管理、测试和使用API的工具,一般用于前后端分离模式,功能类似于Postman

一般流程为:后端人员开发接口并集成Swagger ——> 导出接口文档或直接告知前端人员swagger-ui的URL地址 ——> 前端人员根据接口文档或swagger-ui在线查看接口进行调用获取数据 ——> 后端根据前端需求实时更新接口信息 ——> 项目上线(重点在swagger的使用上,其他环节已经略过~)

Swagger的实时性对于前后端联调是十分友好的

(以上仅为个人理解,如有错误,请各位带佬批评指正!)

二、SpringBoot集成Swagger(Swagger版本2.9.2及以上)

首先请检查你的SpringBoot的版本信息(在pom.xml文件中):

在这里插入图片描述

有两种情况:版本在2.6.0(包含2.6.0)以上、版本在2.6.0以下

2.6.0以上:

在pom.xml中引入如下依赖():
		<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
编辑resource/application.yml文件,添加如下配置:
spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

2.6.0以下:

只需导入依赖即可:
	<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.9.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>

三、Swagger常用注解:

//在Controller层的应用:
//1.在类上应用的注解:
@Api(tags = "这是一个控制器类")
//2.在具体请求方法上的注解:
@ApiOperation(value = "功能总述" , notes = "具体描述")
@ApiParam(value = "请求参数说明")

//在POJO层的应用:
//1.在类上应用的注解:
@ApiModel(description = "XX实体类")
//2.在实体类属性上应用的注解:
@ApiModelProperty(value = "属性说明")

四、访问Swagger-UI:

Swagger版本为2.9.2:

直接访问:localhost:8080/swagger-ui.html

Swagger版本为3.0.0:

直接访问:localhost:8080/swagger-ui/index.html

  • 6
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值