How to create swagger.json

I use the java spring boot framework to create a REST API for my project, and I use "springfox-swagger2 and springfox-swagger-ui" to generate the swagger documentation. I can view my documentation at 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 use a separate application to list the API documentation.

+25
spring-mvc swagger swagger-ui
source share
7 answers

I did it with a little trick

I added the following code at the end of the test case of my home controller

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(); } } } } 

I do not know if this is correct or not. But it works :)

Addiction

  <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> 
+8
source share

You can get the URL from your Swagger-UI HTML page:

enter image description here

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

And in fact, you can get all the urls using the Chrome / Firefox function.

+17
source share

If you use Maven, you can generate documentation on the client side and on the server (yaml, json and html) 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>your-email@email.com</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 eg: "classpath:/markdown.hbs", "classpath:/templates/hello.html" 2) file eg: "${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 the * .hbs template at this address: https://github.com/kongchen/swagger-maven-example

Run mvn swagger: generate the JSon documentation that will be generated in your / genaged / swagger / directory. Go to this address: http://editor.swagger.io

And generate what you want (server side or client side API in your preferred technology)

+9
source share

I'm a little late here, but I just realized that you can open your browser console and find the URL for the GET request, which returns the JSON definition for your Swagger documents. The following method worked for me when mapping my API to the AWS API Gateway.

For this:

  • Go to the endpoint of the Swagger document
  • Open your browser console
  • Refresh the page
  • Go to the network tab and filter by XHR requests.
  • Right-click on an XHR request that ends with ?format=openapi
  • Now you can simply copy and paste it into a new JSON file!
+7
source share

You should be able to get your swagger.json at

http: // localhost: 8080 / api-docs

provided that you have not saved the versions, as in the example of a pet store application. In this case, the URL will be:

http: // localhost: 8080 / v2 / api-docs

+5
source share

To get the JSON API definition for the REST API if swagger is configured correctly. You can use swagger / docs / v1 directly, which means that the full URL will be if version v1 (or just specify the version)

HTTP: // local: 8080 / swag / Docs / v1

0
source share

Someone breaks into MY PART OF THE SYSTEM AND WORKS BY USING THE PHONE WHEN IT GOES THERE ... the system I'm trying to CLOSE ... BUT IT CANNOT DO IT ..

17 FHONE WORK MY FHONE .. So these are not my problems We have a screenshot, let Google know

0
source share

All Articles