我使用java spring引导框架为我的项目创建REST api,并使用“springfox-swagger2和springfox-swagger-ui”来生成swagger文档。我能够使用网址http://localhost:8080/swagger-ui.html查看我的文档。如何生成swagger.json
我如何箱或产生swagger.json/spec.json,该文件不应该使用这个应用程序,我们使用的是上市API文档
我使用java spring引导框架为我的项目创建REST api,并使用“springfox-swagger2和springfox-swagger-ui”来生成swagger文档。我能够使用网址http://localhost:8080/swagger-ui.html查看我的文档。如何生成swagger.json
我如何箱或产生swagger.json/spec.json,该文件不应该使用这个应用程序,我们使用的是上市API文档
我这样做有一个小窍门
我在家里控制器测试用例的末尾添加以下代码
import org.springframework.boot.test.web.client.TestRestTemplate;
public class HomeControllerTest extends .... ...... {
@Autowired
private TestRestTemplate restTemplate;
@Test
public void testHome() throws Exception {
//.......
//... my home controller test code
//.....
String swagger = this.restTemplate.getForObject("/v2/api-docs", String.class);
this.writeFile("spec.json", swagger);
}
public void writeFile(String fileName, String content) {
File theDir = new File("swagger");
if (!theDir.exists()) {
try{
theDir.mkdir();
}
catch(SecurityException se){ }
}
BufferedWriter bw = null;
FileWriter fw = null;
try {
fw = new FileWriter("swagger/"+fileName);
bw = new BufferedWriter(fw);
bw.write(content);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fw != null)
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
我不知道这是正确的方式或不但它的工作:)
依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
如果你使用Maven,您可以通过使用swagger-maven-plugin
添加到您的pom.xml生成客户端和服务器端文件(YAML,JSON和HTML):
.....
<plugin>
<groupId>com.github.kongchen</groupId>
<artifactId>swagger-maven-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<apiSources>
<apiSource>
<springmvc>true</springmvc>
<locations>com.yourcontrollers.package.v1</locations>
<schemes>http,https</schemes>
<host>localhost:8080</host>
<basePath>/api-doc</basePath>
<info>
<title>Your API name</title>
<version>v1</version>
<description> description of your API</description>
<termsOfService>
http://www.yourterms.com
</termsOfService>
<contact>
<email>[email protected]</email>
<name>Your Name</name>
<url>http://www.contact-url.com</url>
</contact>
<license>
<url>http://www.licence-url.com</url>
<name>Commercial</name>
</license>
</info>
<!-- Support classpath or file absolute path here.
1) classpath e.g: "classpath:/markdown.hbs", "classpath:/templates/hello.html"
2) file e.g: "${basedir}/src/main/resources/markdown.hbs",
"${basedir}/src/main/resources/template/hello.html" -->
<templatePath>${basedir}/templates/strapdown.html.hbs</templatePath>
<outputPath>${basedir}/generated/document.html</outputPath>
<swaggerDirectory>generated/swagger-ui</swaggerDirectory>
<securityDefinitions>
<securityDefinition>
<name>basicAuth</name>
<type>basic</type>
</securityDefinition>
</securityDefinitions>
</apiSource>
</apiSources>
</configuration>
</plugin> ........
您可以下载*在这个地址.hbs模板: https://github.com/kongchen/swagger-maven-example
执行MVN招摇:产生 JSON文件将在您的项目/生成/昂首阔步/目录中生成。 过去,该地址: http://editor.swagger.io
,并生成你想要什么都(在你的首选技术,服务器端或客户端API)
我有点晚了这里,但我想通了,你可以打开您的浏览器控制台并找到返回您的Swagger文档的JSON定义的GET请求的URL。将我的API映射到AWS API网关时,以下技术适用于我。
要做到这一点:
?format=openapi
+您必须复制XHR请求的响应正文内容,其中JSON可用 – Vijai
如何d你从这个URL下载swagger.json/spec.json文件? –
在浏览器中键入完整的URL ..并且您得到JSON作为响应。其中可以剪切和粘贴为json文件 –