1.基本使用
springboot版本
-
导包
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency>
-
接口使用注解
@RestController @RequestMapping("/stu") @Api(tags = "学生信息管理") public class StuController { @Autowired private StuService stuService; /** * 添加 * @param stuInfoDto * @return */ @PostMapping @ApiOperation(value = "插入一条信息") public PageResult save(@RequestBody StuInfoDto stuInfoDto) { System.out.println("stu save..."); return stuService.save(stuInfoDto); } /** * 删除 * @param sid * @return */ @DeleteMapping("{sid}") @ApiOperation(value = "删除一条信息") public PageResult delete(@PathVariable Integer sid) { System.out.println("stu delete..." + sid); return stuService.delete(sid); } /** * 更新 * @param stuInfoDto * @return */ @PutMapping @ApiOperation(value = "更新一条信息") public PageResult update(@RequestBody StuInfoDto stuInfoDto) { System.out.println("stu update..."); return stuService.update(stuInfoDto); } /** * 根据sid获取 * @param sid * @return */ @GetMapping("{sid}") @ApiOperation(value = "根据sid查找信息") public PageResult getBySid(@PathVariable Integer sid) { System.out.println("stu getByUserCode..." + sid); return stuService.getBySid(sid); } /** * 获取全部 * @return */ @GetMapping @ApiOperation(value = "查询所有信息") public PageResult getAll() { System.out.println("stu getAll..."); return stuService.getAll(); } }
-
配置swagger
@Configuration @EnableOpenApi public class SwaggerConfig { @Bean public Docket docket(){ return new Docket(DocumentationType.OAS_30) .apiInfo(apiInfo()).enable(true) .select() //添加swagger接口提取范围,修改成指向你的controller包 .apis(RequestHandlerSelectors.basePackage("com.rokned.mypro.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo(){ return new ApiInfoBuilder() .title("学生信息接口文档") .description("学生信息查询系统") .version("1.0") .build(); } }
-
访问连接http://localhost:8999/swagger-ui/index.html即可
如果报错
可能是因为springboot版本高,替换了路径匹配,修改路径匹配配置即可
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
2.kinfe4j框架
使用swagger3
-
导入依赖
<dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>3.0.3</version> </dependency>
-
配置(可选)
@Configuration @EnableOpenApi // @EnableKnife4j // @EnableSwagger2 public class Knife4jConfig { @Bean public Docket docket() { Docket docket = new Docket(DocumentationType.OAS_30) .apiInfo(new ApiInfoBuilder() .title("我的标题") .description("我的描述") // .termsOfServiceUrl("http://www.xx.com/") .contact(new Contact("knife", "https://knife.blog.csdn.net/", "xx@qq.com")) .version("1.0") .build()) // 分组名称 .groupName("all") .select() // 这里指定Controller扫描包路径 .apis(RequestHandlerSelectors.basePackage("com.example.demo")) .paths(PathSelectors.any()) .build(); return docket; } }
-
设置注解
//实体属性: @ApiModelProperty(value = "学生主键",required = true) private Integer sid; @ApiModelProperty("籍贯") private String nativePlace; //controller @Api(tags = "学生信息管理") public class StuController { @ApiOperation(value = "插入一条信息") public PageResult save(@RequestBody StuInfoDto stuInfoDto) { System.out.println("stu save..."); return stuService.save(stuInfoDto); } }
-
启动服务访问http://localhost:8999/doc.htm