哪个网站做外贸好,平面设计广告作品,手机微网站价,建设工程检测预约网站Springfox-swagger使用详解什么是Swagger#xff1f;Swagger的具体使用一、导入依赖二、建立Swagger配置类三、通过Swagger测试接口引用什么是Swagger#xff1f;
是一个开源的API Doc的框架可以将我们的Controller中的API方法以文档的形式展现#xff0c;并支持为其添加注…
Springfox-swagger使用详解什么是SwaggerSwagger的具体使用一、导入依赖二、建立Swagger配置类三、通过Swagger测试接口引用什么是Swagger
是一个开源的API Doc的框架可以将我们的Controller中的API方法以文档的形式展现并支持为其添加注释并实时与后端代码同步我们可以通过Swagger测试后端接口的运行情况这就使得前端人员可以实时看到最新API与后端进行集成联调避免集成问题集中爆发。
Swagger的具体使用
一、导入依赖
dependencygroupIdio.springfox/groupIdartifactIdspringfox-swagger2/artifactIdversion2.9.2/version
/dependency
dependencygroupIdio.springfox/groupIdartifactIdspringfox-swagger-ui/artifactIdversion2.9.2/version
/dependency二、建立Swagger配置类
Configuration
EnableSwagger2 //开启swagger2
public class SwaggerConfig {Beanpublic Docket docket(Environment environment){Profiles profiles Profiles.of(dev);boolean enable environment.acceptsProfiles(profiles); //根据当前开发环境来决定是否要运行swaggerreturn new Docket(DocumentationType.SWAGGER_2).groupName(InformationController) //我们通过建立多个Docket类并给不同的groupname区分不同人员的文档// 修改swagger-ui.html界面的基础显示信息.apiInfo(apiInfo())//判断是否是开发模式来决定是否开启swagger2.enable(enable)// apis 配置扫描接口的方式.select().apis(RequestHandlerSelectors.basePackage(com.wyw.checkinsystem.Controller)) //限定扫描的Controller目录.paths(PathSelectors.ant(/loginstu)) //限定扫描的请求链接.build();//被Controller return的实体类都会显示在model中,我们也可以通过注释标注为Models添加信息//同样我们可以使用另外两个注释为controller中的方法及其参数添加信息}private ApiInfo apiInfo(){Contact contact new Contact(name, , );return new ApiInfo(Ostrich5yw Api Documentation,Api Documentation,1.0,urn:tos,contact,Apache 2.0,http://www.apache.org/licenses/LICENSE-2.0,new ArrayListVendorExtension());} 三、通过Swagger测试接口
我们通过Swagger可以直接填入数据请求来测试Controller中的接口
PS:进入http://localhost:端口号/swagger-ui.html查看swagger日志引用
本文根据遇见狂神说的Docker视频编写。