1.1 简介
swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 restful 风格的 web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。
随着前后端技术的日渐成熟,前后端的交互就只有接口了,前端请求接口获取数据,所以接口的格式化也就相当重要,有一个标准格式的接口文档在开发过程中是相当重要的,swagger就是这么一个在线的接口文档,在springboot的集成之中也相当便利。
swagger可以自动生成在线接口文档,界面可视化的同时保证了便利的测试接口。
创建一个springboot web 项目,然后在pom.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>
可以根据自己的sringboot版本适当降低swagger2 的版本。
在springboot启动类的同级目录下面创建一个config的包,然后创建一个配置swagger2 的配置类。
package com.example.demoswagger.config; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; 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; /** * @author 全栈学习笔记 * @date 2020/4/19 16:00 * @description */ @configuration @enableswagger2 public class swaggerconfig { @bean public docket createrestapi() { return new docket(documentationtype.swagger_2) // 指定构建api文档的详细信息的方法:apiinfo() .apiinfo(apiinfo()) .select() // 指定要生成api接口的包路径 .apis(requesthandlerselectors.basepackage("com.example.demoswagger.controller")) //使用了 @apioperation 注解的方法生成api接口文档 //.apis(requesthandlerselectors.withmethodannotation(apioperation.class)) .paths(pathselectors.any()) //可以根据url路径设置哪些请求加入文档,忽略哪些请求 .build(); } /** * 设置api文档的详细信息 */ private apiinfo apiinfo() { return new apiinfobuilder() // 标题 .title("spring boot集成swagger2") // 接口描述 .description("swagger") // 联系方式 .contact("微信公众号"+"全栈学习笔记"+"359076197@qq.com") // 版本信息 .version("1.0") // 构建 .build(); } }
注意其中的包,不要导错包了。
配置代码说明:
1. 使用 @configuration 注解,标识这是一个配置类,项目启动的时候会自动调用加载,@enableswagger2 注解的作用是自动开启swagger2。
2. apiinfo() 方法里面的参数可以自己设定,在第一个方法中,我们除了可以根据接口所在的包对应生成接口文档还可以根据项目中是否有方法使用了 @apioperation注解来判断是否生成api文档。
上面我们配置好了swagger api生成的配置之后就可以编写测试的controller,创建一个与config同级的包controller,然后里面写一个testcontroller
@restcontroller @requestmapping("/test") @api("测试swagger接口") public class testcontroller { @requestmapping(path = "/getstudent",method = requestmethod.get) @apioperation("/根据学生id获取学生信息") @apiimplicitparam(name = "id",value = "id",required = true,paramtype = "query",datatype = "int") public student getstudent(@requestparam integer id){ student student = new student(); student.setid(11); student.setage(21); student.setname("全栈学习笔记"); map<integer,student> studentmap = new hashmap<>(); studentmap.put(11,student); return studentmap.get(id); } }
其中,student类等会会贴出来。
代码说明:
- header-->放在请求头。请求参数的获取注解:@requestheader
- query -->常用于get请求的参数拼接。请求参数的获取注解:@requestparam
- path -->(用于restful接口)-->请求参数的获取获取注解:@pathvariable
- body -->放在请求体。请求参数的获取注解:@requestbody
@requestmapping(path = "/getstudent",method = requestmethod.patch) @apioperation("/根据学生id获取学生信息") @apiimplicitparams({ @apiimplicitparam(name = "name",value = "姓名",required = true,paramtype = "query",datatype = "string"), @apiimplicitparam(name = "age",value = "年龄",required = true,paramtype = "query",datatype = "int") }) public student editstudent(@requestparam string name, @requestparam integer age){ student student = new student(); student.setid(12); student.setname(name); student.setage(age); return student; }
需要对多个参数进行属性说明时,需要用到 @apiimplicitparams,然后里面再用 @apiimplicitparam。
controller代码
@apioperation("/添加学生信息") @requestmapping(path = "/addstudent",method = requestmethod.post) public map<integer,student> addstudent(@requestbody student student){ map<integer,student> studentmap = new hashmap<>(); studentmap.put(student.getid(),student); return studentmap; }
entity代码:
/** * (student)实体类 * * @author 微信公众号:全栈学习笔记 * @since 2020-04-14 11:39:10 */ @apimodel("学生类") public class student { /** * 唯一标识id */ @apimodelproperty("id") private integer id; /** * 姓名 */ @apimodelproperty("姓名") private string name; /** * 年龄 */ @apimodelproperty(value = "年龄") private integer age; }  省略get,set方法  @apimodelproperty 的属性配置相对之前那个@apiimplicitparam的属性更多更全。
完成以上步骤,带你们看看swagger的界面如何,启动项目,浏览器输入localhost:8091/swagger-ui.html
如下:
打开之后
测试一个接口
结果返回
本期的分享就到这里,文中讲解了swagger2的配置,用法,以及测试,整个流程都讲解的较详细。如果你觉得文章有用,点个赞吧!
转自:https://www.cnblogs.com/swzx-1213/p/12736993.html
如对本文有疑问, 点击进行留言回复!!
Java 生成微信小程序二维码并写入本地(经过反复测试可用)
Spring高级装配,Profile的使用,条件化Bean,解决歧义性
【java基础】面试常见问题:类和对象,封装继承多态,final关键字,static关键字,类加载过程,双亲委派模型
网友评论