资讯专栏INFORMATION COLUMN

Spring Boot [如何优雅的编写文档]

曹金海 / 1300人阅读

摘要:导读在团队协作的时候许多时候需要用到接口文档,我们通常通过手工编写大量重复格式的文档,让我想起了程序员最讨厌的两件事没有文档,编写文档。对应的资料可自行谷歌。关于和官网是这样描述的。我们可以理解为为基于构建的自动生成文档。

导读:

在团队协作的时候许多时候需要用到接口文档,我们通常通过手工编写大量重复格式的文档,让我想起了程序员最讨厌的两件事:没有文档,编写文档。哈哈,如果使用过swagger的朋友应该都很了解它带给我们的便利,如果你还没有使用swagger的话,正好打算编写RESTful API文档,这里有一篇文章Spring Boot中使用Swagger2构建强大的RESTful API文档可以帮助你在较短的时间内构建出一个线上文档,有些时候我们需要生成离线文档有该怎么做呢?带着这个问题我们一起去出发。

关于swagger:

Swagger - 前后端分离后的契约

通过Swagger进行API设计,与Tony Tam的一次对话

一个简短的总结,更好的生成RESTful API文档,提供相应的测试功能,效果图如下:

编写离线文档:

swagger为我们提供了生成在线文档的功能,然而有些时候客户需要的是离线文档的api,有没有什么较好的办法可以通过swagger帮助我们生成离线文档呢?

这就是今天的主角:Springfox和Spring Rest Docs帮我们做的事情
1.预备知识:
建议了解swagger、Asciidoc、asciidoctor-maven-plugin和SpringBoot Testing。对应的资料可自行谷歌。
2.关于Springfox和Spring Rest Docs:
官网是这样描述的Springfox:Automated JSON API documentation for API"s built with Spring。我们可以理解为为 基于Spring构建的API自动生成文档。

引入pom依赖:

其实我们的思路就是把swagger在线文档转成staticdocs形式的文档,引入相关的一些依赖 Spring Rest Docs的依赖spring-restdocs-mockmvc,离线文档的依赖springfox-staticdocs,因为要在单元测试的时候生成文档,所以再加测试相关的spring-boot-starter-test。

</>复制代码

  1. org.springframework.boot
  2. spring-boot-starter-data-jpa
  3. com.h2database
  4. h2
  5. runtime
  6. org.springframework.boot
  7. spring-boot-starter-web
  8. org.springframework.boot
  9. spring-boot-starter-test
  10. io.springfox
  11. springfox-swagger2
  12. 2.6.1
  13. io.springfox
  14. springfox-swagger-ui
  15. 2.6.1
  16. org.springframework.restdocs
  17. spring-restdocs-mockmvc
  18. 1.1.2.RELEASE
  19. test
  20. io.springfox
  21. springfox-staticdocs
  22. 2.6.1
  23. com.alibaba
  24. fastjson
  25. 1.2.8
使用Maven插件:

我们使用asciidoctor-maven-plugin插件将Asciidoc格式转成HTML5格式
了解更多: 使用介绍

</>复制代码

  1. org.asciidoctor
  2. asciidoctor-maven-plugin
  3. ${asciidoctor.html.output.directory}
  4. index.adoc
  5. book
  6. left
  7. 3
  8. ${generated.asciidoc.directory}
  9. output-html
  10. test
  11. process-asciidoc
  12. html
  13. ${project.build.directory}/generated-snippets
编写测试类生成离线文档:

</>复制代码

  1. import cn.sunxyz.domain.UserInfo;
  2. import com.alibaba.fastjson.JSON;
  3. import io.github.robwin.markup.builder.MarkupLanguage;
  4. import io.github.robwin.swagger2markup.GroupBy;
  5. import io.github.robwin.swagger2markup.Swagger2MarkupConverter;
  6. import org.junit.After;
  7. import org.junit.Test;
  8. import org.junit.runner.RunWith;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs;
  11. import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
  12. import org.springframework.boot.test.context.SpringBootTest;
  13. import org.springframework.http.MediaType;
  14. import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation;
  15. import org.springframework.test.context.junit4.SpringRunner;
  16. import org.springframework.test.web.servlet.MockMvc;
  17. import springfox.documentation.staticdocs.SwaggerResultHandler;
  18. import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
  19. import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
  20. import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse;
  21. import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint;
  22. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  23. @AutoConfigureMockMvc
  24. @AutoConfigureRestDocs(outputDir = "target/generated-snippets")
  25. @RunWith(SpringRunner.class)
  26. @SpringBootTest
  27. public class DocumentationBuild {
  28. private String snippetDir = "target/asciidoc/generated-snippets";
  29. private String outputDir = "target/asciidoc";
  30. @Autowired
  31. private MockMvc mockMvc;
  32. @After
  33. public void Test() throws Exception {
  34. // 得到swagger.json,写入outputDir目录中
  35. mockMvc.perform(get("/v2/api-docs").accept(MediaType.APPLICATION_JSON))
  36. .andDo(SwaggerResultHandler.outputDirectory(outputDir).build())
  37. .andExpect(status().isOk())
  38. .andReturn();
  39. // 读取上一步生成的swagger.json转成asciiDoc,写入到outputDir
  40. // 这个outputDir必须和插件里面标签配置一致
  41. Swagger2MarkupConverter.from(outputDir + "/swagger.json")
  42. .withPathsGroupedBy(GroupBy.TAGS)// 按tag排序
  43. .withMarkupLanguage(MarkupLanguage.ASCIIDOC)// 格式
  44. .withExamples(snippetDir)
  45. .build()
  46. .intoFolder(outputDir);// 输出
  47. }
  48. @Test
  49. public void TestApi() throws Exception {
  50. mockMvc.perform(get("/api/user/1")
  51. .accept(MediaType.APPLICATION_JSON))
  52. .andExpect(status().isOk())
  53. .andDo(MockMvcRestDocumentation.document("查询用户", preprocessResponse(prettyPrint())));
  54. UserInfo userInfo = new UserInfo();
  55. userInfo.setName("lisi");
  56. userInfo.setAge(23);
  57. userInfo.setAddress("山东济南");
  58. userInfo.setSex("男");
  59. mockMvc.perform(post("/api/user").contentType(MediaType.APPLICATION_JSON)
  60. .content(JSON.toJSONString(userInfo))
  61. .accept(MediaType.APPLICATION_JSON))
  62. .andExpect(status().is2xxSuccessful())
  63. .andDo(MockMvcRestDocumentation.document("新增用户", preprocessResponse(prettyPrint())));
  64. }
  65. }

由于前面已经配置了maven的插件,只需要执行测试就可以生成相应的文档, 效果图如下:

补充:

Swagger配置:

</>复制代码

  1. @Configuration
  2. @EnableSwagger2
  3. public class SwaggerConfiguration {
  4. @Bean
  5. public Docket configSpringfoxDocket_all(ApiInfo apiInfo) {
  6. return new Docket(DocumentationType.SWAGGER_2)
  7. .produces(Sets.newHashSet("application/json"))
  8. .consumes(Sets.newHashSet("application/json"))
  9. .protocols(Sets.newHashSet("http", "https"))
  10. .apiInfo(apiInfo)
  11. .forCodeGeneration(true)
  12. .select().paths(regex("/api.*"))
  13. .build();
  14. }
  15. @Bean
  16. public Docket createUserInfoRestApi(ApiInfo apiInfo) {
  17. return new Docket(DocumentationType.SWAGGER_2)
  18. .groupName("user")
  19. .produces(Sets.newHashSet("application/json"))
  20. .consumes(Sets.newHashSet("application/json"))
  21. .protocols(Sets.newHashSet("http", "https"))
  22. .apiInfo(apiInfo)
  23. .select()
  24. .apis(RequestHandlerSelectors.basePackage("cn.sunxyz.controller"))
  25. .paths(regex("/api/user.*"))
  26. .build();
  27. }
  28. @Bean
  29. public ApiInfo apiInfo() {
  30. return new ApiInfoBuilder().title("Springfox REST API")
  31. .description("Descriptions.")
  32. .termsOfServiceUrl("http://springfox.io")
  33. .license("Apache License Version 2.0")
  34. .licenseUrl("https://github.com/springfox/springfox/blob/master/LICENSE")
  35. .version("2.0")
  36. .build();
  37. }
  38. }

相关源码已托管github

参考资料:

Spring REST Docs

SpringBoot项目生成RESTfull API的文档

Introduction to Spring REST Docs

asciidoctor-maven-plugin

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/70000.html

相关文章

  • Spring Boot 中 crud如何优雅实现-附代码

    摘要:以下内容基于如果你使用的也是相同的技术栈可以继续往下阅读,如果不是可以当作参考。编写的四种方式裸写最简单最粗暴也是使用最多的一种方式,在写的多了之后可以用生成工具生成。 导读 在目前接触过的项目中大多数的项目都会涉及到: crud相关的操作, 哪如何优雅的编写crud操作呢?带着这个问题,我们发现项目中大量的操作多是 创建实体 、删除实例、 修改实体、 查询单个实体、 分页查询多个实体...

    wing324 评论0 收藏0
  • 如何优雅关闭 Spring Boot 应用

    摘要:除了,还有十余种,有的是特定操作,比如转储内存日志有的是信息展示,比如显示应用健康状态。 showImg(http://ww1.sinaimg.cn/large/006tNc79gy1g5qb2coyfoj30u00k0tan.jpg); 前言 随着线上应用逐步采用 SpringBoot 构建,SpringBoot应用实例越来多,当线上某个应用需要升级部署时,常常简单粗暴地使用 kil...

    xiyang 评论0 收藏0
  • Spring Boot 参考指南(开发你第一个Spring Boot应用程序)

    摘要:开发你的第一个应用程序本节描述如何开发一个简单的应用程序来突出了的一些关键特性,我们使用来构建这个项目,因为大多数都支持它。如果你希望分发一个自包含的应用程序,这可能会有问题。 11. 开发你的第一个Spring Boot应用程序 本节描述如何开发一个简单的Hello World! web应用程序来突出了Spring Boot的一些关键特性,我们使用Maven来构建这个项目,因为大多数...

    Cristalven 评论0 收藏0
  • @ConfigurationProperties 注解使用姿势,这一篇就够了

    摘要:在项目中,为满足以上要求,我们将大量的参数配置在或文件中,通过注解,我们可以方便的获取这些参数值使用配置模块假设我们正在搭建一个发送邮件的模块。这使得在不影响其他模块的情况下重构一个模块中的属性变得容易。 在编写项目代码时,我们要求更灵活的配置,更好的模块化整合。在 Spring Boot 项目中,为满足以上要求,我们将大量的参数配置在 application.properties 或...

    SolomonXie 评论0 收藏0
  • @ConfigurationProperties 注解使用姿势,这一篇就够了

    摘要:在项目中,为满足以上要求,我们将大量的参数配置在或文件中,通过注解,我们可以方便的获取这些参数值使用配置模块假设我们正在搭建一个发送邮件的模块。这使得在不影响其他模块的情况下重构一个模块中的属性变得容易。 在编写项目代码时,我们要求更灵活的配置,更好的模块化整合。在 Spring Boot 项目中,为满足以上要求,我们将大量的参数配置在 application.properties 或...

    KoreyLee 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<