我正在使用Dropwizard创建我的服务和REST API,并在我的项目中添加了dropwizard-swagger,以便可以在UI上轻松访问此API。我正在创建一个CLI工具,它将使用openapi-generator在UI端生成我的API的Typescript模型:
openapi-generator generate -g typescript-fetch -i http://localhost:8080/swagger.json -o ~/Desktop/test
但是,我需要swagger.json定义,并且当前正在从路径http://localhost:8080/swagger.json的GET请求中获取这些定义。我想通过CLI Java项目以编程方式获取swagger.json(如何在不启动服务器的情况下获取swagger规范)。我正在寻找以编程方式生成swagger.json的方法,并尝试使用swagger-maven-plugin:
<plugin>
<groupId>com.github.kongchen</groupId>
<artifactId>swagger-maven-plugin</artifactId>
<version>3.1.4</version>
<configuration>
<apiSources>
<apiSource>
<springmvc>false</springmvc>
<locations>
<location>me.pablo.api.LauraPortraitsRestMethods</location>
</locations>
<schemes>http,https</schemes>
<host>http://localhost:8080/</host>
<basePath>/swagger</basePath>
<info>
<title>My project</title>
<version>v1</version>
<description>Do this late</description>
<termsOfService>
http://www.github.com/kongchen/swagger-maven-plugin
</termsOfService>
<contact>
<email>[email protected]</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}/generated-sources/strapdown.html.hbs</templatePath>
<outputPath>${basedir}/generated-sources/document.html</outputPath>
<swaggerDirectory>${basedir}/generated-sources/</swaggerDirectory>
</apiSource>
</apiSources>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
以及Rest方法的类
package me.pabloestrada.api;
import com.google.inject.Inject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
@Path("/laura")
@Api(value = "/laura")
public class LauraPortraitsRestMethods
{
private LauraPortraitsService delegate;
@Inject
public LauraPortraitsRestMethods(final LauraPortraitsService delegate) {
this.delegate = delegate;
}
@GET
@ApiOperation(value = "Getting developer name")
@Path("/dev")
public String getDeveloperName() {
return delegate.getDeveloperName();
}
}
[当我运行mvn clean compile时,看不到任何东西生成。我看不到swagger-maven-plugin的任何输出。有没有人有任何建议可以完成这项工作,或者有其他方法以编程方式获取swagger.json?]
如果有人还在寻找,请将其添加到您的Maven构建插件中:
<plugin>
<groupId>com.github.kongchen</groupId>
<artifactId>swagger-maven-plugin</artifactId>
<version>3.1.8</version>
<configuration>
<apiSources>
<apiSource>
<springmvc>false</springmvc>
<locations>
<location>me.pabloestrada.api.*</location>
</locations>
<schemes>http,https</schemes>
<outputFormats>json</outputFormats>
<basePath>/</basePath>
<host>127.0.0.1:8000</host>
<info>
<title>APIs</title>
<version>1.0.0</version>
<description>APIs</description>
<contact>
<email>[email protected]</email>
<name>Support</name>
</contact>
<license>
<url>http://www.apache.org/licenses/LICENSE-2.0.html</url>
<name>Apache 2.0</name>
</license>
</info>
<swaggerDirectory>swagger</swaggerDirectory>
<swaggerFileName>swaggerspec</swaggerFileName>
</apiSource>
</apiSources>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
它将创建一个swagger目录,并在该目录中创建一个包含swaggerspec的json文件,名为swaggerspec.json。