码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 17:第二章:架构后端项目:13:Swagger2接口文档工具;(在【imooc-news-dev-service-api】这个模块上,配置)


    说明:

    (1)本篇博客内容:Swagger2接口文档工具;

    (2)声明: 

              ● 在【Spring Boot电商项目25:商品分类模块四:使用Swagger自动生成API文档;】中,我们就介绍过Swagger2了;可以去参考;但是,本篇博客的内容,会有些微差别;不过还好,两篇博客都是可行策略;;;两篇博客,对比着瞅瞅吧;

              ● 按理说,我们应该在所有的微服务中,都去构建一Swagger的配置;但这样太麻烦了;;;又因为,已知我们所有微服务的接口,都会被【api】统一管理起来,所以我们在【api】中配置Swagger2,就会很很省事;

    目录

    一:正式开发;

    1.首先,在【imooc-news-dev-service-api】中,引入swagger2相关依赖;

    2.然后,在在【imooc-news-dev-service-api】中,创建config包,并创建Swagger的配置类:去配置Swagger;

    3.然后,在【imooc-news-dev-service-api】的接口上,利用@Api()去设置接口说明;

    4.也可以在在【imooc-news-dev-service-api】的接口中的,方法上,使用@ApiOperation(),去具体给某个方法设置说明;

    二:测试;


    一:正式开发;

    1.首先,在【imooc-news-dev-service-api】中,引入swagger2相关依赖;

    1. <!--引入swagger2相关依赖-->
    2. <dependency>
    3. <groupId>io.springfox</groupId>
    4. <artifactId>springfox-swagger2</artifactId>
    5. </dependency>
    6. <dependency>
    7. <groupId>io.springfox</groupId>
    8. <artifactId>springfox-swagger-ui</artifactId>
    9. </dependency>
    10. <dependency>
    11. <groupId>com.github.xiaoymin</groupId>
    12. <artifactId>swagger-bootstrap-ui</artifactId>
    13. </dependency>

    说明:

    (1)在【api】模块中,引入swagger2相关依赖;

    2.然后,在在【imooc-news-dev-service-api】中,创建config包,并创建Swagger的配置类:去配置Swagger;

    Swagger2类:

    1. package com.imooc.api.config;
    2. import com.google.common.base.Predicate;
    3. import com.google.common.base.Predicates;
    4. import org.springframework.context.annotation.Bean;
    5. import org.springframework.context.annotation.Configuration;
    6. import springfox.documentation.RequestHandler;
    7. import springfox.documentation.builders.ApiInfoBuilder;
    8. import springfox.documentation.builders.PathSelectors;
    9. import springfox.documentation.builders.RequestHandlerSelectors;
    10. import springfox.documentation.service.ApiInfo;
    11. import springfox.documentation.service.Contact;
    12. import springfox.documentation.spi.DocumentationType;
    13. import springfox.documentation.spring.web.plugins.Docket;
    14. import springfox.documentation.swagger2.annotations.EnableSwagger2;
    15. @Configuration //使用这个注解,说明这是个配置类;
    16. @EnableSwagger2//使用【@EnableSwagger2注解】:去开启Swagger功能;
    17. public class Swagger2 {
    18. // http://localhost:服务端口号/swagger-ui.html 原路径
    19. // http://localhost:服务端口号/doc.html 原路径
    20. // 配置swagger2核心配置 docket
    21. @Bean
    22. public Docket createRestApi() {
    23. // Predicate<RequestHandler> adminPredicate = RequestHandlerSelectors.basePackage("com.imooc.admin.controller");
    24. // Predicate<RequestHandler> articlePredicate = RequestHandlerSelectors.basePackage("com.imooc.article.controller");
    25. Predicate<RequestHandler> userPredicate = RequestHandlerSelectors.basePackage("com.imooc.user.controller");
    26. // Predicate<RequestHandler> filesPredicate = RequestHandlerSelectors.basePackage("com.imooc.files.controller");
    27. return new Docket(DocumentationType.SWAGGER_2) // 指定api类型为swagger2
    28. .apiInfo(apiInfo()) // 用于定义api文档汇总信息
    29. .select()
    30. .apis(Predicates.or(userPredicate))//因为,目前我们只开发了【user】,所以这儿我们这儿只保留user的;(adminPredicate, articlePredicate, userPredicate, filesPredicate)
    31. .paths(PathSelectors.any()) // 所有controller
    32. .build();
    33. }
    34. private ApiInfo apiInfo() {
    35. return new ApiInfoBuilder()
    36. .title("慕课新闻·自媒体接口api") // 文档页标题
    37. .contact(new Contact("imooc",
    38. "https://www.imooc.com",
    39. "abc@imooc.com")) // 联系人信息
    40. .description("专为慕课新闻·自媒体平台提供的api文档") // 详细信息
    41. .version("1.0.1") // 文档版本号
    42. .termsOfServiceUrl("https://www.imooc.com") // 网站地址
    43. .build();
    44. }
    45. }

    说明:

    (1)需要根据我们项目的包结构,去设置扫描包名,要确保扫描到【api】工程包含的所有微服务的接口;

    3.然后,在【imooc-news-dev-service-api】的接口上,利用@Api()去设置接口说明;

    4.也可以在在【imooc-news-dev-service-api】的接口中的,方法上,使用@ApiOperation(),去具体给某个方法设置说明;


    二:测试;

    运行【imooc-news-dev-service-user】的主启动类;

    然后,通过【http://localhost:8003/swagger-ui.html】就可以访问了;

    然后,通过【http://localhost:8003/doc.html】也是可以访问的;

    PS:Swagger是个很好的工具了,客户,前端,经理,自己都可以使用;

  • 相关阅读:
    【linux命令讲解大全】014.Git:分布式版本控制系统的先驱和常用命令清单(三)
    web前端面试题附答案002-说一下react组件间的数据传递
    CloudKit提示Permission Failure:Invalid bundle ID for container 错误的超详细解决
    前、后端通用的可视化逻辑编排
    Gitea 与 Jenkins 的集成实践,打造你的专属 CI/CD 系统
    如何编译linux驱动ko
    纯js写一个弹窗
    Python网络编程之TCP编程
    项目架构设计说明书编制
    Linux(八)——解压缩
  • 原文地址:https://blog.csdn.net/csucsgoat/article/details/125462941
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号