Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate swagger.json [duplicate]

I am using java spring boot framework to create REST api for my project and I am using "springfox-swagger2 and springfox-swagger-ui" for generating swagger documentation. I am able to see my documentation using the URL http://localhost:8080/swagger-ui.html.

How can I create or generate swagger.json / spec.json, The documentation should not be with this application, we are using a separate application for listing the API docs.

like image 466
Bikesh M Avatar asked Jan 23 '17 14:01

Bikesh M


People also ask

Is swagger JSON auto generated?

Swagger AUTOMATIC Documentation Generation: Just throw a few lines above the resource definition and the documentation (json) is automatically generated by the module.

How do I get JSON from Swagger UI?

If you don't see the url or if url is a code expression, open the browser dev tools, switch to the Network tab and disable caching. Then refresh the page and search for the API definition file ( swagger. json , swagger. yaml , api-docs or similar) among HTTP requests.


2 Answers

You can get the url with your swagger-ui html page:

enter image description here

GET http://localhost:8080/v2/api-docs?group=App 

And actually you can get all the urls with chrome/firefox develop tools network feature.

like image 101
Liping Huang Avatar answered Sep 22 '22 13:09

Liping Huang


If you use Maven, you can generate client and server side documentation (yaml, json and html) by using swagger-maven-plugin

Add this to your pom.xml:

.....  <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> ........ 

You can download *.hbs template at this address: https://github.com/kongchen/swagger-maven-example

Execute mvn swagger:generate JSon documentation will be generated at your project /generated/swagger/ directory. Past it on this address : http://editor.swagger.io

And generate what ever you want ( Server side or Client side API in your preferred technology )

like image 22
MK-rou Avatar answered Sep 24 '22 13:09

MK-rou