1,概念
Swagger 是一个规范且完整的框架,用于生成、形貌、调用和可视化 RESTful 风格的 Web 服务。
2,使用swagger优势
支持 API 自动生成同步的在线文档:使用 Swagger 后可以直接通过代码生成文档,不再需要自己手动编写接口文档了,对步伐员来说非常方便,可以节流写文档的时间去学习新技术。
提供 Web 页面在线测试 API:光有文档还不敷,Swagger 生成的文档还支持在线测试。参数和格式都定好了,直接在界面上输入参数对应的值即可在线测试接口。
3,接下来开始整合,使用springboot项目,有web框架支持即可
4,需要的依赖
使用2.9.2版本稍微稳定一些
- io.springfox springfox-swagger-ui 2.9.2 io.springfox springfox-swagger2 2.9.2
复制代码 5,新建一个config包,再新建一个SwaggerTest类
- package com.example.demo.config;import org.springframework.context.annotation.Configuration;import springfox.documentation.swagger2.annotations.EnableSwagger2;@Configuration //点进源码可以发现该注解等价于Component//开启swagger2@EnableSwagger2public class SwaggerTest {}
复制代码 6,运行项目,并在欣赏器中输入http://localhost:8080/swagger-ui.html,出现以下界面,项目就初始化好了
7,设置属于自己的文档
由于在Docket类下的所有参数都是使用默认的参数(点击Docket源码即可知),正如上图界面中的参数都是默认的,因此可以重写类内里的方法以及修改方法中的参数
- package com.example.demo.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.env.Environment;import org.springframework.core.env.Profiles;import springfox.documentation.builders.PathSelectors;import springfox.documentation.builders.RequestHandlerSelectors;import springfox.documentation.service.ApiInfo;import springfox.documentation.service.Contact;import springfox.documentation.spi.DocumentationType;import springfox.documentation.spring.web.plugins.Docket;import springfox.documentation.swagger2.annotations.EnableSwagger2;import java.util.ArrayList;@Configuration //点进源码可以发现该注解等价于Component//开启swagger2@EnableSwagger2public class SwaggerTest { @Bean public Docket docket(){ * DocumentationType.SWAGGER_2:点进Docket可以发现构造方法中需要一个参数, * 在点进这个参数类DocumentationType就能发现详细的参数了 */ return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()) //是否启动swagger,如果参数为false,那么欣赏器则克制访问swagger //.enable(true) .groupName("郑辉盛") //.select ** .build为一套模板 .select() //RequestHandlerSelectors:设置要扫描接口的方式, // basePackage:可以指定要扫描的包 // any:扫描全部 //none:不扫描 //withMethodAnnotation:扫描方法上的注解 .apis(RequestHandlerSelectors.basePackage("com.example.demo")) //过滤什么路径 //.paths(PathSelectors.ant("/demo/**")) .build(); } //设置swagger信息apiInfo private ApiInfo apiInfo(){ //作者信息 Contact contact = new Contact("郑辉盛","","1652724084@qq.com"); return new ApiInfo( "郑辉盛的swagger文档", //标题 "纵然再小的帆也能远航", //形貌 "1.0", //版本 "https://blog.csdn.net/zhenghuishengq", contact, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList()); }}
复制代码 8,再次运行,可以得到自己所设置的参数的界面了,右上角出现的谁人为组名,就是.groupName(“郑辉盛”)所设置的
9,如果想多个人同时开发,就可以设置多个组名,因此需要多个Docket对象,可以在上诉代码中参加
- @Bean public Docket docket2(){ return new Docket(DocumentationType.SWAGGER_2).groupName("a"); } @Bean public Docket docket3(){ return new Docket(DocumentationType.SWAGGER_2).groupName("b"); } @Bean public Docket docket4(){ return new Docket(DocumentationType.SWAGGER_2).groupName("c"); } @Bean public Docket docket5(){ return new Docket(DocumentationType.SWAGGER_2).groupName("d"); }
复制代码 10,再次测试,可以发现多出了几个模块
11,以自己的模块举行测试,新建一个pojo包,并再建一个实体类User
- package com.example.demo.pojo;import io.swagger.annotations.ApiModel;import io.swagger.annotations.ApiModelProperty;//给生成的文档注释@ApiModel("用户实体类")public class User { @ApiModelProperty("用户名") public String name; @ApiModelProperty("暗码") public String password; public User(String name, String password) { this.name = name; this.password = password; } public User(){}}
复制代码 12,新建一个controller包,新建一个HelloController类
- package com.example.demo.controller;import com.example.demo.pojo.User;import io.swagger.annotations.Api;import io.swagger.annotations.ApiOperation;import io.swagger.annotations.ApiParam;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RestController;@Api("HelloController控制类")@RestController //没有界面,返回一个字符串public class HelloController { @GetMapping(value = "/hello") public String hello(){ return "hello"; } @GetMapping(value = "/user") public User getUser(){ return new User("郑辉盛","123456"); } @ApiOperation("hello控制类") @PostMapping("/hello2") public String hello2(@ApiParam("用户名") String userName){ return "hello" + userName; } @ApiOperation("Post测试类") @PostMapping("/hello3") public User hello3(@ApiParam("用户名") User user){ return user; }}
复制代码 13,直接运行,
在Models中可以发现实体类,以及生成的文档注释
再次点开hello-controller
14,接下来到了告急有刺激的开发人员测试环节
点开/hello3这个接口,并点击try it out,最后执行
最终出现以下界面,说明接口测试乐成,没问题!
15,总结
可以通过swagger给一些比力难懂白的属性大概接口,增加注释信息
接口文档实时更新
可以在线测试
16,如需要源代码,可以直接访问:https://gitee.com/zhenghuisheng/swagger
来源:https://blog.csdn.net/zhenghuishengq/article/details/112043869
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |