• swagger3+Apifox生成接口文档


    在springboot后端项目中,自己在生成接口文档时花了不少时间,在此总结一下,帮助其他小伙伴~

    swagger3使用

    一、pom.xml中添加依赖

    1. io.springfox
    2. springfox-boot-starter
    3. 3.0.0
         并将springboot改为低于3.0版本
    1. org.springframework.boot
    2. spring-boot-starter-parent
    3. 2.6.1
         在application.properties里加上一句配置语句,否则会报错
         "Failed to start bean 'documentationPluginsBootstrapper';"
    spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER
    

    二、创建一个配置类SwaggerConfig.java

    1. package com.example.demo.config;
    2. import org.springframework.context.annotation.Bean;
    3. import org.springframework.context.annotation.Configuration;
    4. import springfox.documentation.builders.PathSelectors;
    5. import springfox.documentation.builders.RequestHandlerSelectors;
    6. import springfox.documentation.oas.annotations.EnableOpenApi;
    7. import springfox.documentation.service.ApiInfo;
    8. import springfox.documentation.service.Contact;
    9. import springfox.documentation.spi.DocumentationType;
    10. import springfox.documentation.spring.web.plugins.Docket;
    11. import javax.servlet.http.HttpServletRequest;
    12. import javax.servlet.http.HttpServletResponse;
    13. import javax.servlet.http.HttpSession;
    14. import java.util.ArrayList;
    15. @Configuration
    16. @EnableOpenApi
    17. public class SwaggerConfig {
    18. //apiInfo类,介绍作者信息,随便写就行
    19. private ApiInfo apiInfo(){
    20. Contact contact = new Contact("lcf","https://www.baidu.com/","123@qq.com");
    21. return new ApiInfo(
    22. "接口文档示例",
    23. "再小的帆也能远航",
    24. "v1.0",
    25. "https://scs.buaa.edu.cn/",
    26. contact,
    27. "Apache 2.0",
    28. "http://www.apache.org/licenses/LICENSE-2.0",
    29. new ArrayList());
    30. }
    31. @Bean
    32. public Docket account() {
    33. String regex="(?i).*/account/.*";
    34. return new Docket(DocumentationType.SWAGGER_2)
    35. .apiInfo(apiInfo())
    36. //生成接口文档时,忽略以下三种参数类型,一般调试接口用不到
    37. .ignoredParameterTypes(HttpSession.class, HttpServletRequest.class, HttpServletResponse.class)
    38. .select()
    39. .apis(RequestHandlerSelectors.any())
    40. .paths(PathSelectors.regex(regex))//只扫描文件路径含有account的文件
    41. .build()
    42. .groupName("account");
    43. }
    44. }

     三、创建一个接口类AccountController

    1. package com.example.demo.controller;
    2. import org.springframework.web.bind.annotation.RequestMapping;
    3. import org.springframework.web.bind.annotation.RequestMethod;
    4. import org.springframework.web.bind.annotation.RestController;
    5. @RestController
    6. public class AccountController {
    7. @RequestMapping(value = "/account/hello", method = RequestMethod.POST)
    8. public String Hello(String name){
    9. return "hello"+name;
    10. }
    11. }

    四、启动项目,打开swagger的ui界面

    启动项目,没有报错

    打开swagger-ui界面,这里我的地址是

    http://localhost:8080/swagger-ui/index.html

    成功!开始导出接口文档

    Apifox导出接口文档

    进入个人项目后,点击更多功能-->导入

    返回刚才的swagger-ui界面,右键-->检查-->network-->刷新页面

    找到以api-docs开头的这个url,复制:右键-->copy-->copy link address

    这里我的url是:

    http://localhost:8080/v2/api-docs?group=account

    url就复制好啦,然后打开apifox,导入数据类型选择OpenAPI/Swagger

    下方选择以url方式导入,粘贴刚才复制的url

    点击确定导入,成功。

    ps:

    一、swagger的参数识别

    默认条件下,swagger会将接口的参数识别为请求体(body)里的参数,而我们想要的是查询参数(param)

    所以,需要在每个接口里自定义参数的类型,加上@RequestParam注解就行

    反之,如果想要swagger识别成请求体(body)里的参数,在该参数前加上注解 @RequestBody 

    二.Apifox使用:

    右上角这里,设置环境,默认服务,前置url设为本地环境:localhost:8080,就可以进行本地调试了

    返回个人项目,点击在线分享,就能分享给前端小伙伴了

  • 相关阅读:
    C陷阱与缺陷 第7章 可移植性缺陷 7.1 应对C语言标准变更
    什么是CRM系统?为什么现代企业都在使用?
    卷积神经网络笔记
    优化器算法
    单点登录SSO
    设计模式之适配器模式
    java在线问答平台计算机毕业设计MyBatis+系统+LW文档+源码+调试部署
    CSP-J第二轮试题-2022年-1.2题
    后App时代的小程序化技术?
    webrtc终极版(一)5分钟搭建多人实时会议系统webrtc
  • 原文地址:https://blog.csdn.net/m0_62113397/article/details/132619414