发布于 2018-03-24 05:26:31 | 486 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Spring Framework 开源j2ee框架

Spring是什么呢?首先它是一个开源的项目,而且目前非常活跃;它是一个基于IOC和AOP的构架多层j2ee系统的框架,但它不强迫你必须在每一层 中必须使用Spring,因为它模块化的很好,允许你根据自己的需要选择使用它的某一个模块;它实现了很优雅的MVC,对不同的数据访问技术提供了统一的接口,采用IOC使得可以很容易的实现bean的装配,提供了简洁的AOP并据此实现Transcation Managment,等等


这篇文章将带你了解如何用spring官方推荐的restdoc去生成api文档。具有一定的参考价值,感兴趣的小伙伴们可以参考一下

这篇文章将带你了解如何用spring官方推荐的restdoc去生成api文档。本文创建一个简单的springboot工程,将http接口通过Api文档暴露出来。只需要通过 JUnit单元测试和Spring的MockMVC就可以生成文档。

准备工作

  1. 你需要15min
  2. Jdk 1.8
  3. maven 3.0+
  4. idea

创建工程

引入依赖,其pom文件:


<dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.springframework.restdocs</groupId>
      <artifactId>spring-restdocs-mockmvc</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

通过@SpringBootApplication,开启springboot


@SpringBootApplication
public class Application {

  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}

在springboot通常创建一个controller:


@RestController
public class HomeController {

  @GetMapping("/")
  public Map<String, Object> greeting() {
    return Collections.singletonMap("message", "Hello World");
  }

}

启动工程,访问localhost:8080,浏览器显示:


{“message”:”Hello World”}

证明接口已经写好了,但是如何通过restdoc生存api文档呢

Restdoc,通过单元测试生成api文档

restdocs是通过单元测试生存snippets文件,然后snippets根据插件生成htm文档的。

建一个单元测试类:


@RunWith(SpringRunner.class)
@WebMvcTest(HomeController.class)
@AutoConfigureRestDocs(outputDir = "target/snippets")
public class WebLayerTest {

  @Autowired
  private MockMvc mockMvc;

  @Test
  public void shouldReturnDefaultMessage() throws Exception {
    this.mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk())
        .andExpect(content().string(containsString("Hello World")))
        .andDo(document("home"));
  }
}

其中,@ AutoConfigureRestDocs注解开启了生成snippets文件,并指定了存放位置。

启动单元测试,测试通过,你会发现在target文件下生成了一个snippets文件夹,其目录结构如下:


└── target
  └── snippets
    └── home
      └── httpie-request.adoc
      └── curl-request.adoc
      └── http-request.adoc
      └── http-response.adoc

默认情况下,snippets是Asciidoctor格式的文件,包括request和reponse,另外其他两种httpie和curl两种流行的命令行的http请求模式。

到目前为止,只生成了Snippets文件,需要用Snippets文件生成文档。

怎么用Snippets

创建一个新文件src/main/asciidoc/index.adoc :


用 Spring REST Docs 构建文档

This is an example output for a service running at http://localhost:8080:

.request
include::{snippets}/home/http-request.adoc[]

.response
include::{snippets}/home/http-response.adoc[]

这个例子非常简单,通过单元测试和一些简单的配置就能够得到api文档了。

adoc的书写格式,参考:http://docs.spring.io/spring-restdocs/docs/current/reference/html5/,这里不多讲解。

需要使用asciidoctor-maven-plugin插件,在其pom文件加上:


<plugin>
  <groupId>org.asciidoctor</groupId>
  <artifactId>asciidoctor-maven-plugin</artifactId>
  <executions>
    <execution>
      <id>generate-docs</id>
      <phase>prepare-package</phase>
      <goals>
        <goal>process-asciidoc</goal>
      </goals>
      <configuration>
        <sourceDocumentName>index.adoc</sourceDocumentName>
        <backend>html</backend>
        <attributes>
          <snippets>${project.build.directory}/snippets</snippets>
        </attributes>
      </configuration>
    </execution>
  </executions>
</plugin>

这时只需要通过mvnw package命令就可以生成文档了。

在/target/generated-docs下有个index.html,打开这个html,显示如下,界面还算简洁:

结语

通过单元测试,生存adoc文件,再用adoc文件生存html,只需要简单的几步就可以生成一个api文档的html文件,这个html文件你可以通网站发布出去。整个过程很简单,对代码无任何影响。

源码下载:https://github.com/forezp/SpringBootLearning

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持PHPERZ。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务