企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# Swagger Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。 总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法、参数和模型紧密集成到服务器端的代码,允许 API 来始终保持同步。Swagger 让部署管理和使用功能强大的 API 从未如此简单。 ***** ### pom.xml中引入maven ``` <!--swagger--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${swagger.version}</version> </dependency> <!--swagger ui--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>${swagger.version}</version> </dependency> <swagger.version>2.7.0</swagger.version> ``` ***** ### 新建配置类 ``` @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket webApiConfig(){ return new Docket(DocumentationType.SWAGGER_2) .groupName("webApi") .apiInfo(webApiInfo()) .select() .paths(Predicates.not(PathSelectors.regex("/admin/.*"))) .paths(Predicates.not(PathSelectors.regex("/error.*"))) .build(); } private ApiInfo webApiInfo(){ return new ApiInfoBuilder() .title("网站-课程中心API文档") .description("本文档描述了课程中心微服务接口定义") .version("1.0") .contact(new Contact("Helen", "http://atguigu.com", "55317332@qq.com")) .build(); } } ``` ***** ### 定义接口说明和参数说明 #### controller ``` @Api(tags = "讲师管理") @RestController @RequestMapping("/eduservice/edu-teacher") public class EduTeacherController { @ApiOperation(value = "分页带条件查询") @PostMapping("pageTeacherCondition/{current}/{limit}") public R pageTeacherCondition(@ApiParam(name = "current",value = "当前页",required = true) @PathVariable Long current, @ApiParam(name = "limit",value = "每页数",required = true) @PathVariable Long limit, @RequestBody(required = false) TeacherQuery teacherQuery){ //创建page对象 Page<EduTeacher> pageTeacher = new Page<>(current,limit); QueryWrapper<EduTeacher> eduTeacherQueryWrapper = new QueryWrapper<>(); String name = teacherQuery.getName(); Integer level = teacherQuery.getLevel(); String begin = teacherQuery.getBegin(); String end = teacherQuery.getEnd(); if(!StringUtils.isEmpty(name)){ eduTeacherQueryWrapper.like("name",name); } if(!StringUtils.isEmpty(level)){ eduTeacherQueryWrapper.like("level",level); } if(!StringUtils.isEmpty(begin)){ eduTeacherQueryWrapper.like("name",begin); } if(!StringUtils.isEmpty(end)){ eduTeacherQueryWrapper.like("name",end); } IPage<EduTeacher> page = iEduTeacherService.page(pageTeacher, eduTeacherQueryWrapper); long total = page.getTotal(); List<EduTeacher> records = page.getRecords(); return R.ok().data("page",page).data("total",total).data("records",records); } } ``` #### bean ``` @ApiModel(value = "Teacher查询对象", description = "讲师查询对象封装") @Data public class TeacherQuery { private static final long serialVersionUID = 1L; @ApiModelProperty(value = "教师名称,模糊查询") private String name; @ApiModelProperty(value = "头衔 1高级讲师 2首席讲师") private Integer level; @ApiModelProperty(value = "查询开始时间", example = "2019-01-01 10:10:10") private String begin;//注意,这里使用的是String类型,前端传过来的数据无需进行类型转换 @ApiModelProperty(value = "查询结束时间", example = "2019-12-01 10:10:10") private String end; } ``` ***** ### 结果测试 固定输入地址为:http://127.0.0.1:port/swagger-ui.html 其中port为程序允许端口 打开后主页为 ![](https://img.kancloud.cn/30/b3/30b3229038db210fe5c2be205e6528f6_1022x533.jpg) 点击想测试的api,并输入测试数据 ![](https://img.kancloud.cn/87/13/8713b27f1a608a1573605a2ba2f6b84b_775x812.jpg) 点击测试,观察返回结果 ![](https://img.kancloud.cn/f0/e6/f0e616ab15656bb588df1b067cf8349b_775x769.jpg)