,
一、斯瓦格是什么? Swagger是用于生成、说明、调用和可视化rest风格的Web服务的规范和完整框架。
总体目标是将客户端和文件系统作为服务器以相同的速度更新。 的方法、参数和模型紧密集成到服务器端代码中,以确保API始终保持同步。 Swagger使部署管理和强大的API的使用比以往任何时候都更简单。
二、swagger的使用方法? 本文主要介绍swagger在springboot中的使用。
1、部署依赖: dependencygroupidio.spring fox/groupidartifactidspringfox-swagger2/artifactidversion2.9.2/version/epeped groupidartifactidspringfox-swagger-ui/artifactidversion2.9.2/version /还是强大的。
2、配置swagger在config包下创建新的class类。
@ configuration @ enable swagger2publicclassswaggerconfig { @ beanpublicdocketdocket (environment environment ) /设置要显示的swagger 返回新文档(文档类型. swagger _2).APIinfo(APIinfo ).groupname ) ' Daodao ' ); 在flag中,即使该环境访问swagger//.enable(flag )//.select )//any ) :并扫描所有//none ) :也可以参数为注释的反射对象(restController )//withMethodAnnotation :扫描注释(/.APIS ) requesthandlerselectors.withmethodation 公共API info API info () { Contact contact=new Contact )、(localhost:8080 )、(123455@qq.com); return new ApiInfo (“刀”、“再小的帆也能远行”、“1.0”、“localhost:8080”、contact、“apache2.0”、“wangjiansuoyyon 请尝试登录http://localhost :8080/swagger-ui.html
3、建立简单的测试实体类:
@ data @ allargsconstructor @ noargsconstructor @ API model (描述='新闻') publicclassnewsimplementsserializable { }控制层(不写服务层和dao层,简单查询)。
@ restcontrollerpublicclassnewscontroller { @ autowiredprivateinewsservicenewsservice; @getmapping(/news ) ) public List queryAll ) throwsexception ) modelandviewm=newmodelandview ); listnewsnewslist=news service.query all (; 返回新列表; //测试方法@postmapping(/newsbytitle ) ) publicstringquerybytitle ) API param @ requestbodystringtitle ) throwsexceptiong stiong stititle 系统. out.println (content ); 返回内容; }请测试一下:
一般来说,swagger的基本用法就是这样的。 不是很难。
那么,您也可以看到swagger的优点。 对于分离前后端、在线自动生成文档等非常有用。 但是,swagger必须隐藏或删除,主要是在生产环境中。 否则,会有安全隐患。
4、swagger常用评论常用评论:
@用于API ()类;
方法使用资源@ API操作()来指示此类是swagger;
表示http请求的操作@ApiParam ()用于说明方法、参数和字段;
将元数据添加到参数(例如,说明或是否必需) @ApiModel )用于类
介绍类,用于参数实体类接收@ApiModelProperty ()方法,用于字段
表示model属性说明或数据操作更改的@ApiIgnore ()用于类、方法和方法参数
表示此方法或类被忽略的@ApiImplicitParam ()用于该方法
指示方法使用单个请求参数@ApiImplicitParams ()并包含多个@ApiImplicitParam