spring boot rest接口自动生成文档(包含swagger)
2017-06-22 19:28
1331 查看
spring boot rest接口自动生成文档(包含swagger)
写接口免不了写接口文档,但是当文档与代码分开独立演进的时候,会发生很多不同步的问题。接口描述与代码同步的最简便的方式就是利用java doc,改代码或参数的时候,顺便把java doc也更新了。
所以,我们可以利用java doc自动生成文档。
组件enunciate 就是这种方式来自动生成文档,而且生成了一份html文档,也包括swagger ui。
下面例子介绍了此组件的maven插件使用方式,方便自动生成文档,打包jar包内。
目前为止,最新版本:
<enunciate.version>2.9.1</enunciate.version>
不过,支持enum,和 oauth2(swagger)不是太好,作者正在改进。
本项目以spring boot:
pom 添加依赖:
<dependency>
<groupId>com.webcohesion.enunciate</groupId>
<artifactId>enunciate-core</artifactId>
<version>${enunciate.version}</version>
</dependency>
<dependency>
<groupId>com.webcohesion.enunciate</groupId>
<artifactId>enunciate-core-annotations</artifactId>
<version>${enunciate.version}</version>
</dependency>
添加插件:
<plugin>
<groupId>com.webcohesion.enunciate</groupId>
<artifactId>enunciate-maven-plugin</artifactId>
<version>${enunciate.version}</version>
<executions>
<execution>
<goals>
<goal>assemble</goal>
</goals>
<configuration>
<docsDir>${project.basedir}/src/main/resources/static/doc</docsDir>
<configFile>enunciate.xml</configFile>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.webcohesion.enunciate</groupId>
<artifactId>enunciate-lombok</artifactId>
<version>${enunciate.version}</version>
</dependency>
</dependencies>
</plugin>
因为项目中使用了lombok,所以配置了enunciate-lombok,上面enunciate.xml为enunciate的配置文件,
<docsDir>${project.basedir}/src/main/resources/static/doc</docsDir>为文档生成目录,放到此目录下,spring
boot静态资源可以正常访问。
enunciate.xml 的配置内容:
<enunciate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://enunciate.webcohesion.com/schemas/enunciate-2.2.0.xsd">
<title>航空文档 作者: xxxx</title>
<copyright>https://www.xxx.com</copyright>
<application root="/airline"/>
<api-classes>
<include pattern="com.xxx.airline.controller.**"/>
</api-classes>
<modules>
<jackson1 disabled="true"/>
</modules>
</enunciate>
对项目执行打包命令:mvn clean install
生成的文档:
本地访问以下:http://localhost:8999/airline/doc/index.html
airline 为tomcat 上下文路径。
swagger ui界面:
具体个人demo,见:
https://github.com/sdcuike/spring-boot-oauth2-demo/tree/blog-2017-06-22/spring-boot-oauth-resource-server
相关文章推荐
- spring boot 中使用swagger 来自动生成接口文档
- Spring Boot(九)Swagger2自动生成接口文档和Mock模拟数据
- Swagger自动接口文档生成框架————springboot整合swagger总结
- springboot结合swagger自动生成接口文档
- springboot+springfox+Swagger 实现项目的restful文档的自动生成
- 使用 SpringBoot + Swagger 生成接口 API 文档
- SpringBoot&Swagger构建REST API并生成API文档
- Spring boot结合swagger自动生成api文档
- SpringBoot + mybatis + Swagger快速构建REST API并生成优美的API文档
- Spring Boot如何让Web API自动生成文档,并解决swagger-annotations的API注解description属性废弃的问题
- 使用Spring Boot&Swagger快速构建REST API并生成优美的API文档
- Spring Rest Docs WebTestClient自动生成接口文档Gradle版
- SwaggerUI自动生成API文档(SwaggerUI+SpringBoot)
- Swagger+Spring mvc生成Restful接口文档
- Swagger+Spring mvc生成Restful接口文档
- Spring MVC+ swagger-bootstrap-ui生成接口文档
- 使用Swagger生成Spring Boot REST客户端(支持Feign)(待实践)
- Swagger(webapi自动生成接口说明文档)
- Swagger 一款RESTFUL接口的文档在线自动生成+功能测试功能软件
- 一款RESTFUL接口的文档在线自动生成+功能测试功能软件——Swagger简介