213

Recently I wrote restful APIs with SpringMvc and swagger-ui(v2). I noticed the Import function in Postman:

enter image description here

So my question is how to create the file which Postman needed?

I am not familiar with Swagger.

Moha the almighty camel
  • 4,327
  • 4
  • 30
  • 53
Demon Coldmist
  • 2,560
  • 2
  • 13
  • 21

8 Answers8

204

I work on PHP and have used Swagger 2.0 to document the APIs. The Swagger Document is created on the fly (at least that is what I use in PHP). The document is generated in the JSON format.

Sample document

{
    "swagger": "2.0",
    "info": {
    "title": "Company Admin Panel",
        "description": "Converting the Magento code into core PHP and RESTful APIs for increasing the performance of the website.",
        "contact": {
        "email": "jaydeep1012@gmail.com"
        },
        "version": "1.0.0"
    },
    "host": "localhost/cv_admin/api",
    "schemes": [
    "http"
],
    "paths": {
    "/getCustomerByEmail.php": {
        "post": {
            "summary": "List the details of customer by the email.",
                "consumes": [
                "string",
                "application/json",
                "application/x-www-form-urlencoded"
            ],
                "produces": [
                "application/json"
            ],
                "parameters": [
                    {
                        "name": "email",
                        "in": "body",
                        "description": "Customer email to ge the data",
                        "required": true,
                        "schema": {
                        "properties": {
                            "id": {
                                "properties": {
                                    "abc": {
                                        "properties": {
                                            "inner_abc": {
                                                "type": "number",
                                                    "default": 1,
                                                    "example": 123
                                                }
                                            },
                                            "type": "object"
                                        },
                                        "xyz": {
                                        "type": "string",
                                            "default": "xyz default value",
                                            "example": "xyz example value"
                                        }
                                    },
                                    "type": "object"
                                }
                            }
                        }
                    }
                ],
                "responses": {
                "200": {
                    "description": "Details of the customer"
                    },
                    "400": {
                    "description": "Email required"
                    },
                    "404": {
                    "description": "Customer does not exist"
                    },
                    "default": {
                    "description": "an \"unexpected\" error"
                    }
                }
            }
        },
        "/getCustomerById.php": {
        "get": {
            "summary": "List the details of customer by the ID",
                "parameters": [
                    {
                        "name": "id",
                        "in": "query",
                        "description": "Customer ID to get the data",
                        "required": true,
                        "type": "integer"
                    }
                ],
                "responses": {
                "200": {
                    "description": "Details of the customer"
                    },
                    "400": {
                    "description": "ID required"
                    },
                    "404": {
                    "description": "Customer does not exist"
                    },
                    "default": {
                    "description": "an \"unexpected\" error"
                    }
                }
            }
        },
        "/getShipmentById.php": {
        "get": {
            "summary": "List the details of shipment by the ID",
                "parameters": [
                    {
                        "name": "id",
                        "in": "query",
                        "description": "Shipment ID to get the data",
                        "required": true,
                        "type": "integer"
                    }
                ],
                "responses": {
                "200": {
                    "description": "Details of the shipment"
                    },
                    "404": {
                    "description": "Shipment does not exist"
                    },
                    "400": {
                    "description": "ID required"
                    },
                    "default": {
                    "description": "an \"unexpected\" error"
                    }
                }
            }
        }
    },
    "definitions": {

    }
}

This can be imported into Postman as follow.

  1. Click on the 'Import' button in the top left corner of Postman UI.
  2. You will see multiple options to import the API doc. Click on the 'Paste Raw Text'.
  3. Paste the JSON format in the text area and click import.
  4. You will see all your APIs as 'Postman Collection' and can use it from the Postman.

Importing the JSON into Postman

Imported APIs

You can also use 'Import From Link'. Here paste the URL which generates the JSON format of the APIs from the Swagger or any other API Document tool.

This is my Document (JSON) generation file. It's in PHP. I have no idea of JAVA along with Swagger.

<?php
require("vendor/autoload.php");
$swagger = \Swagger\scan('path_of_the_directory_to_scan');
header('Content-Type: application/json');
echo $swagger;
JDpawar
  • 3,474
  • 2
  • 21
  • 26
  • 2
    thanks, but now the problem is how could I export the file from swagger-ui?And the link is useless. – Demon Coldmist Aug 22 '16 at 08:59
  • @DemonColdmist I have added the code to generate the API. Basically, it scans the entire directory, checks the annotations and makes the JSON/YAML output. Sorry, but I haven't used Swagger with JAVA. – JDpawar Aug 22 '16 at 09:28
  • thanks, if it could be export in PHP, so do Java. I will translate it into Java. – Demon Coldmist Aug 23 '16 at 00:54
  • 2
    In a java app using the springfox-swagger2 dependency, you can get the JSON to import in Postman as described in this answer by opening a browser and heading to http://localhost:8080/v2/api-docs – Nacho Mezzadra Jan 05 '18 at 04:37
  • 1
    @JDpawar Thanks, the import succeed, but it's not generating any 'body' info in postman for any POST API. any ideas? – user1559625 Dec 06 '18 at 04:51
100

With .Net Core it is now very easy:

  1. You go and find JSON URL on your swagger page:

enter image description here

  1. Click that link and copy the URL
  2. Now go to Postman and click Import:

enter image description here

  1. Select what you need and you end up with a nice collection of endpoints:

enter image description here

Mik
  • 3,998
  • 2
  • 26
  • 16
13

The accepted answer is correct but I will rewrite complete steps for java.

I am currently using Swagger V2 with Spring Boot 2 and it's straightforward 3 step process.

Step 1: Add required dependencies in pom.xml file. The second dependency is optional use it only if you need Swagger UI.

        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

Step 2: Add configuration class

@Configuration
@EnableSwagger2
public class SwaggerConfig {

     public static final Contact DEFAULT_CONTACT = new Contact("Usama Amjad", "https://stackoverflow.com/users/4704510/usamaamjad", "hello@email.com");
      public static final ApiInfo DEFAULT_API_INFO = new ApiInfo("Article API", "Article API documentation sample", "1.0", "urn:tos",
              DEFAULT_CONTACT, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList<VendorExtension>());

    @Bean
    public Docket api() {
        Set<String> producesAndConsumes = new HashSet<>();
        producesAndConsumes.add("application/json");
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(DEFAULT_API_INFO)
                .produces(producesAndConsumes)
                .consumes(producesAndConsumes);

    }
}

Step 3: Setup complete and now you need to document APIs in controllers

    @ApiOperation(value = "Returns a list Articles for a given Author", response = Article.class, responseContainer = "List")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "Success"),
            @ApiResponse(code = 404, message = "The resource you were trying to reach is not found") })
    @GetMapping(path = "/articles/users/{userId}")
    public List<Article> getArticlesByUser() {
       // Do your code
    }

Usage:

You can access your Documentation from http://localhost:8080/v2/api-docs just copy it and paste in Postman to import collection.

enter image description here

Optional Swagger UI: You can also use standalone UI without any other rest client via http://localhost:8080/swagger-ui.html and it's pretty good, you can host your documentation without any hassle.

enter image description here

UsamaAmjad
  • 4,175
  • 3
  • 28
  • 35
  • 3
    Error while importing: Error while importing Swagger 2.0: (Patchable) parameter.type is mandatory for non-body parameters – Ramraj Jul 17 '19 at 10:28
1

Recommend you to read this answer

https://stackoverflow.com/a/51072071/4712855

Refer to the https://stackoverflow.com/posts/39072519 answer, and then partially delete the returned content. Finally, it is found that swagger lacks some configuration and postmat cannot be imported.

You need to add the following configuration in swagger

@Bean
public Docket api(SwaggerProperties swaggerProperties) {
 swaggerProperties.setTitle("my-project");
 swaggerProperties.setDescription("my project");
 swaggerProperties.setVersion("v1");
 swaggerProperties.getContact().setUrl("http");
 //I overlooked other configurations. Note that my swagger configuration lacks these.
}

In short, the attributes in the ApiInfoBuilder class are assigned values as much as possible

spring-boot version:2.3.10.RELEASE springfox-swagger version: 2.9.2

取一个好的名字
  • 1,677
  • 14
  • 16
1

This is what worked for me from the Swagger editor interface:

Method 1

Copy the YAML file contents into the Raw Text area:

enter image description here

Method 2 (more steps)

Step 1: Export the file as JSON

enter image description here

Step 2: Import the JSON file with Postman "Import"

enter image description here

Aakash
  • 21,375
  • 7
  • 100
  • 81
0

You can do that: Postman -> Import -> Link -> {root_url}/v2/api-docs

logbasex
  • 1,688
  • 1
  • 16
  • 22
-1
  • Click on the orange button ("choose files")
  • Browse to the Swagger doc (swagger.yaml)
  • After selecting the file, a new collection gets created in POSTMAN. It will contain folders based on your endpoints.

You can also get some sample swagger files online to verify this(if you have errors in your swagger doc).

-2

I have same problem strapi side. I just get the json from network.

Get Swagger JSON from Network

enter image description here

Get this json and save it as file.

enter image description here

Import you saved json file before.

enter image description here

enter image description here

enter image description here

enter image description here

Click number 1 then 2. You will see the entire api in postman. enter image description here

Baris Senyerli
  • 642
  • 7
  • 13