使用swagger生成API的json文件
2016-06-01 09:54
555 查看
使用swagger生成API的json文件
使用swagger可以方便的生成REST API,最近有机会了解一下,就记录下小小的踩坑经历吧.demo使用maven搭建,REST采用jersey,swagger的版本选用了新版(即io.swagger),接着就是要用到的swagger-maven-plugin了.
在pom.xml中添加plugin
<plugin> <groupId>com.github.kongchen</groupId> <artifactId>swagger-maven-plugin</artifactId> <version>3.1.0</version> <configuration> <apiSources> <apiSource> <springmvc>false</springmvc> <!-- false为JAX-RS --> <locations>com.rooyeetone.rtprest.rest</locations> <schemes>http,https</schemes> <host>petstore.swagger.wordnik.com</host> <basePath>/rtprest/rest</basePath> <info> <title>RTP REST APIs</title> <version>v1</version> <description>This is a sample for swagger-maven-plugin</description> <termsOfService> http://www.github.com/kongchen/swagger-maven-plugin </termsOfService> <contact> <email>kongchen@gmail.com</email> <name>Kong Chen</name> <url>http://kongch.com</url> </contact> <license> <url>http://www.apache.org/licenses/LICENSE-2.0.html</url> <name>Apache 2.0</name> </license> </info> <templatePath>${basedir}/src/main/resources/templates/strapdown.html.hbs</templatePath> <outputPath>${basedir}/src/main/resources/generated/document.html</outputPath> <swaggerDirectory>${basedir}/src/main/resources/generated/swagger-ui</swaggerDirectory> <!-- <attachSwaggerArtifact>true</attachSwaggerArtifact> --> <!-- <jsonExampleValues>true</jsonExampleValues> --> </apiSource> </apiSources> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>generate</goal> </goals> </execution> </executions> </plugin>
官网给的版本3.0.1没下载到,所以换为3.1.0,等maven下载完依赖可能会报错,在
<execution>标签处会提示错误,可以选择quickfix,ignore,然后打开
lifecycle-mapping-metadata.xml文件,将
<action>标签修改,完整xml如下
<?xml version="1.0" encoding="UTF-8"?> <lifecycleMappingMetadata> <pluginExecutions> <pluginExecution> <pluginExecutionFilter> <groupId>com.github.kongchen</groupId> <artifactId>swagger-maven-plugin</artifactId> <versionRange>3.1.0</versionRange> <goals> <goal>generate</goal> </goals> </pluginExecutionFilter> <action> <execute> <runOnIncremental>true</runOnIncremental> </execute> </action> </pluginExecution> </pluginExecutions> </lifecycleMappingMetadata>
maven update ,然后执行命令
mvn clean compile
查看生成的json吧,如果如果只有swagger的info,那么需要注意plugin3.1.0对应的swagger版本为新版,使用的注解应同时存在
@Api和
@Path
wan,有时间再补充吧
相关文章推荐
- maven学习
- maven插件maven-war-plugin的使用
- jenkins------部署项目到jboss eap下
- maven使用经验集
- 解析Silverlight调用WCF/Rest异常的解决方法
- python实现rest请求api示例
- 深入学习JavaScript中的Rest参数和参数默认值
- java application maven项目打自定义zip包实例(推荐)
- window下安装和配置maven环境
- Eclipse使用maven搭建spring mvc图文教程
- 使用maven+eclipse搭建struts2开发环境
- 基于Java中两种jersey文件上传方式
- myeclipse中使用maven前常见错误及解决办法
- Java随手笔记8之包、环境变量和访问控制及maven profile实现多环境打包
- maven利用tomcat插件部署远程Linux服务器的步骤详解
- maven搭建java ee项目图文教程
- Java的项目构建工具Maven的配置和使用教程
- maven中下载jar包源码和javadoc的命令介绍
- 基于JAVA中Jersey处理Http协议中的Multipart的详解