前言技术之swagger
创始人
2024-05-09 15:33:47
0

一.前后端分离的特点

前后端分离是的前端与后端之间的职责更加明确

后台: 负责业务处理

前端: 负责显示逻辑

在这种情况下,前端和后端可以分别交付给专业的开发人员去做,所以是必须要定义前后端直接的对接

接口,否则各自为是则项目无法集成,这时就需要一个文档来定义统一的接口。

二.在没有swagger之前

在没有swagger之间,我们可以使用word,excel等功能来书写接口定义文档,但又有一个弊端,即:

在接口发送改变时需要及时的同步接口文档,否则实际的接口与接口文档不相符,则接口文件就失去了

作用,甚至会起到反作用。

三.swagger的作用

根据在代码中使用自定义的注解来生成接口文档,这个在前后端分离的项目中很重要。这样做的好处是

在开发接口时可以通过swagger将接口文档定义好,同时也方便以后的维护。

四.swagger的优点

号称时最流行的API框架

接口文档在线生成,避免同步的麻烦

可以支持在线对接口执行测试

支持多语言

五.集成swagger

5.1 新建springboot项目

使用集成开发工具创建一个springboot工程

5.2 集成swagger

1.pom.xml



io.springfox
swagger
2.9.2



io.springfox
springfox-swagger-ui
2.9.2

2.编写swagger配置类

package com.ycx.swagger.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;@Configuration
@EnableSwagger2
public class SwaggerConfig {@Beanpublic Docket createRestApi(){return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)).paths(PathSelectors.ant("/**")) //项目名.build();}private ApiInfo apiInfo() {return new ApiInfoBuilder().title("SwaggerDemoAPIDOC").description("SwaggerDemoAPIDOC").version("1.0").termsOfServiceUrl("https://www.baidu.com").build();}
}

注意:SpringBoot与swagger2的版本对应关系,否则项目是启动不成功的,这里的版本对应关系如下

5.3 开发一个controller用于测试

package com.ycx.swagger.web;import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;
import java.util.Map;//@Api(tags = {"这是swagger入门类","作用2"})
@Api(tags = "这是swagger入门类")
@RestController
@RequestMapping("/swagger")
public class HelloController {
//    @ApiOperation(value = "", tags = "swagger入门方法")@ApiOperation(value = "swagger入门方法")@GetMapping("/hello")public Map hello(){Map map = new HashMap<>();map.put("code",200);map.put("msg","响应成功!!!");return map;}
}

5.4 启动服务,验证集成效果

服务启动后,访问:http://localhost:8080/swagger-ui.html

说明集成成功

6.swagger常用注解


注解

位置

作用

参数

@Api

标识这个类是swagger的资源

tags:说明该类的作用,参数是个数组,可 以填多个。

value="该参数没什么意义,在UI界面上不显示,所以不用配置"

description = "用户基本信息操作"

@ApiOperation

方法

表示一个http请求的操作

value="方法的用途和作用"

notes="方法的注意事项和备注"

tags:说明该方法的作用,参数是个数组,可以填多 个。

格式:tags={"作用1","作用2"}

@ApiParam

方法,参数

对参数使用说明(如:说明 或是否必填等)

value="用户名" 描述参数的意义

name="name" 参数的变量名

required=true 参数是否必选

@ApiModel

表示对类进行说明,用于参 数用实体类接收,一般用在 DTO上

description="描述实体的作用"

@ApiModelProperty

方法,字段

表示对model属性的说明

value="用户名" 描述参 数的意义

name="name" 参数的变量名

required=true 参数是否必选

@ApiIgnore

类,方法,参数

表示这个方法或者类被忽略

@ApiImplicitParams

方法

包含多@ApiImplicitParam

@ApiImplicitParam

方法

表示单独的请求参数

name="参数名称"

value="参数说明"

dataType="数据类型"

paramType="query" 表示参数放在哪里

defaultValue="参数的默认值"

required="true" 表示参数是否必须传

paramType="query"的解释如下

header 请求参数的获取:@RequestHeader

query 请求参数的获取:@RequestParam

path(用于restful接口) 请求参数的获取:@PathVariable

body(不常用)

form(不常用)

更全面的信息可以参考官方说明文档:

https://docs.swagger.io/swagger-core/apidocs/index.html

    • swagger使用综合案例

package com.ycx.swagger.web;import com.ycx.swagger.dto.User;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;import java.util.HashMap;
import java.util.Map;@RestController
@RequestMapping("/swagger/api")
@Api(tags = "swagger所有注解的讲解")
public class SwaggerController {@ApiOperation(value = "欢迎信息")@GetMapping("/hello")@ApiImplicitParams({@ApiImplicitParam(name = "name", value = "名称", dataType = "string", paramType = "query", required = true),@ApiImplicitParam(name = "msg", value="消息", dataType = "string", paramType = "query", required = true)})public Object hello(String name, String msg) {Map map = new HashMap<>();map.put("code", 200);map.put("msg", "操作成功");map.put("info",name+":"+msg);return map;}@PostMapping("/register")@ApiOperation("注册用户接口")@ApiResponses({@ApiResponse(code = 5001001,message = "错误1"),@ApiResponse(code = 5001002,message = "错误2"),@ApiResponse(code = 5001003,message = "错误3")})public Object register(User user) {Map map = new HashMap<>();map.put("code", 5001002);map.put("msg", "操作成功");map.put("info",user);return map;}@PutMapping("/edit")@ApiOperation("修改用户信息")public Object edit(@RequestBody User user) {Map map = new HashMap<>();map.put("code", 200);map.put("msg", "操作成功");map.put("info",user);return map;}@DeleteMapping("/delete/{id}")@ApiOperation("删除用户")@ApiImplicitParam(name = "id", value="用户ID", dataType = "string", paramType = "path", required = true)public Object delete(@PathVariable("id") String id) {Map map = new HashMap<>();map.put("code", 200);map.put("msg", "操作成功");map.put("info",id);return map;}
}

package com.ycx.swagger.dto;import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;@Data
@ApiModel(description = "用户信息")
public class User {@ApiModelProperty(value = "用户名", name="name", required = true)private String name;@ApiModelProperty(value = "密码", name="passwd", required = true)private String passwd;}

    • 会议OA之swagger

  1. pom.xml


4.0.0org.springframework.bootspring-boot-starter-parent2.7.7 com.ycxminoa0.0.1-SNAPSHOTminoaDemo project for Spring Boot1.81.2.702.9.8org.springframework.bootspring-boot-starter-jdbcorg.springframework.bootspring-boot-starter-weborg.mybatis.spring.bootmybatis-spring-boot-starter2.2.1mysqlmysql-connector-java5.1.44runtimeorg.projectlomboklomboktruecom.alibabafastjson${fastjson.version}io.springfoxspringfox-swagger22.9.2io.springfoxspringfox-swagger-ui2.9.2org.springframework.bootspring-boot-maven-pluginorg.projectlomboklombokorg.mybatis.generatormybatis-generator-maven-plugin1.3.2mysqlmysql-connector-java${mysql.version}true

2.swagger的整合配置类

package com.ycx.minoa.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;@Configuration
@EnableSwagger2
public class SwaggerConfig {@Beanpublic Docket createRestApi(){return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)).paths(PathSelectors.ant("/**")) //项目名.build();}private ApiInfo apiInfo() {return new ApiInfoBuilder().title("SwaggerDemoAPIDOC").description("SwaggerDemoAPIDOC").version("1.0").termsOfServiceUrl("https://www.baidu.com").build();}
}

注意:此时SpringBoot (2.7.7) 版本与swagger2 (2.9.2) 的版本不兼容,需要添加application.yml相关配置

3.application.yml

spring:mvc:pathmatch:matching-strategy: ant_path_matcher

4.web层接口服务提供

package com.ycx.minoa.wxcontroller;import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@Api(tags = "会议OA接口开发测试")
@RestController
public class HelloController {@GetMapping("/hello")public String hello(){return "hello";}
}
@Api(tags = "会议OA小程序接口列表")
@RestController
@RequestMapping("/wx/home")
public class WxHomeController {@Autowiredprivate InfoMapper infoMapper;@ApiOperation("会议OA首页数据加载")@GetMapping("/index")public Object index(@RequestBody Info info) {List infoList = infoMapper.list(info);Map data = new HashMap();data.put("infoList",infoList);return ResponseUtil.ok(data);}
}

package com.ycx.minoa.model;import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;import java.util.Date;@ApiModel(description = "会议信息")
public class Info {@ApiModelProperty(value = "会议ID", name="id", required = false)private Long id;@ApiModelProperty(value = "会议标题", name="title", required = false)private String title;@ApiModelProperty(value = "会议内容", name="content", required = false)private String content;@ApiModelProperty(value = "参与者", name="canyuze", required = false)private String canyuze;@ApiModelProperty(value = "列席人员", name="liexize", required = false)private String liexize;@ApiModelProperty(value = "主持人", name="zhuchiren", required = false)private String zhuchiren;@ApiModelProperty(value = "会议地点", name="location", required = false)private String location;@ApiModelProperty(value = "会议开始时间", name="starttime", required = false)private Date starttime;@ApiModelProperty(value = "会议结束时间", name="endtime", required = false)private Date endtime;@ApiModelProperty(value = "附件", name="fujian", required = false)private String fujian;@ApiModelProperty(value = "会议状态", name="state", required = false)private Integer state;@ApiModelProperty(value = "审批人", name="auditperson", required = false)private String auditperson;@ApiModelProperty(value = "审批时间", name="audittime", required = false)private Date audittime;@ApiModelProperty(value = "会议座位图片", name="seatpic", required = false)private String seatpic;@ApiModelProperty(value = "备注", name="remark", required = false)private String remark;public Info(Long id, String title, String content, String canyuze, String liexize, String zhuchiren, String location, Date starttime, Date endtime, String fujian, Integer state, String auditperson, Date audittime, String seatpic, String remark) {this.id = id;this.title = title;this.content = content;this.canyuze = canyuze;this.liexize = liexize;this.zhuchiren = zhuchiren;this.location = location;this.starttime = starttime;this.endtime = endtime;this.fujian = fujian;this.state = state;this.auditperson = auditperson;this.audittime = audittime;this.seatpic = seatpic;this.remark = remark;}public Info() {super();}public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}public String getCanyuze() {return canyuze;}public void setCanyuze(String canyuze) {this.canyuze = canyuze;}public String getLiexize() {return liexize;}public void setLiexize(String liexize) {this.liexize = liexize;}public String getZhuchiren() {return zhuchiren;}public void setZhuchiren(String zhuchiren) {this.zhuchiren = zhuchiren;}public String getLocation() {return location;}public void setLocation(String location) {this.location = location;}public Date getStarttime() {return starttime;}public void setStarttime(Date starttime) {this.starttime = starttime;}public Date getEndtime() {return endtime;}public void setEndtime(Date endtime) {this.endtime = endtime;}public String getFujian() {return fujian;}public void setFujian(String fujian) {this.fujian = fujian;}public Integer getState() {return state;}public void setState(Integer state) {this.state = state;}public String getAuditperson() {return auditperson;}public void setAuditperson(String auditperson) {this.auditperson = auditperson;}public Date getAudittime() {return audittime;}public void setAudittime(Date audittime) {this.audittime = audittime;}public String getSeatpic() {return seatpic;}public void setSeatpic(String seatpic) {this.seatpic = seatpic;}public String getRemark() {return remark;}public void setRemark(String remark) {this.remark = remark;}
}

5.测试结果

相关内容

热门资讯

美国不提安卓系统华为,迈向自主... 华为与美国:一场关于技术、市场与政策的较量在当今这个数字化的世界里,智能手机已经成为我们生活中不可或...
安卓系统怎么打开ppt,选择文... 你有没有遇到过这种情况:手里拿着安卓手机,突然需要打开一个PPT文件,却怎么也找不到方法?别急,今天...
谷歌退回到安卓系统,探索创新未... 你知道吗?最近科技圈可是炸开了锅,谷歌竟然宣布要退回到安卓系统!这可不是一个简单的决定,背后肯定有着...
安卓系统待机耗电多少,深度解析... 你有没有发现,手机电量总是不经用?尤其是安卓系统,有时候明明没怎么用,电量就“嗖”的一下子就下去了。...
小米主题安卓原生系统,安卓原生... 亲爱的手机控们,你是否曾为手机界面单调乏味而烦恼?想要给手机换换“衣服”,让它焕然一新?那就得聊聊小...
voyov1安卓系统,探索创新... 你有没有发现,最近你的手机是不是变得越来越流畅了?没错,我要说的就是那个让手机焕发青春的Vivo V...
电脑刷安卓tv系统,轻松打造智... 你有没有想过,家里的安卓电视突然变得卡顿,反应迟钝,是不是时候给它来个“大保健”了?没错,今天就要来...
安卓系统即将要收费,未来手机应... 你知道吗?最近有个大消息在科技圈里炸开了锅,那就是安卓系统可能要开始收费了!这可不是开玩笑的,这可是...
雷凌车载安卓系统,智能出行新体... 你有没有发现,现在的汽车越来越智能了?这不,我最近就体验了一把雷凌车载安卓系统的魅力。它就像一个聪明...
怎样拍照好看安卓系统,轻松拍出... 拍照好看,安卓系统也能轻松搞定!在这个看脸的时代,拍照已经成为每个人生活中不可或缺的一部分。无论是记...
安卓车机系统音频,安卓车机系统... 你有没有发现,现在越来越多的汽车都开始搭载智能车机系统了?这不,咱们就来聊聊安卓车机系统在音频方面的...
老苹果手机安卓系统,兼容与创新... 你手里那台老苹果手机,是不是已经陪你走过了不少风风雨雨?现在,它竟然还能装上安卓系统?这可不是天方夜...
安卓系统7.dns,优化网络连... 你有没有发现,你的安卓手机最近是不是有点儿“慢吞吞”的?别急,别急,让我来给你揭秘这可能与你的安卓系...
安卓手机系统怎么加速,安卓手机... 你有没有发现,你的安卓手机最近变得有点“慢吞吞”的?别急,别急,今天就来给你支几招,让你的安卓手机瞬...
小米note安卓7系统,探索性... 你有没有发现,手机更新换代的速度简直就像坐上了火箭呢?这不,小米Note这款手机,自从升级到了安卓7...
安卓和鸿蒙系统游戏,两大系统游... 你有没有发现,最近手机游戏界可是热闹非凡呢!安卓和鸿蒙系统两大巨头在游戏领域展开了一场激烈的较量。今...
安卓手机没有系统更,揭秘潜在风... 你有没有发现,现在安卓手机的品牌和型号真是五花八门,让人挑花了眼。不过,你知道吗?尽管市面上安卓手机...
充值宝带安卓系统,安卓系统下的... 你有没有发现,最近手机上的一款充值宝APP,在安卓系统上可是火得一塌糊涂呢!这不,今天就来给你好好扒...
安卓系统8.0镜像下载,轻松打... 你有没有想过,想要给你的安卓手机升级到最新的系统,却不知道从哪里下载那个神秘的安卓系统8.0镜像呢?...
安卓系统修改大全,全方位修改大... 你有没有想过,你的安卓手机其实是个大宝藏,里面藏着无数可以让你手机焕然一新的秘密?没错,今天就要来个...