最近我写了RESTful API中与用SpringMVC和招摇的UI(V2).今天,我只注意到按钮导入的postman.And我点击它上面,我刚才看到的评论" 导入邮差收集,环境,数据转储,卷曲命令,或RAML/WADL/Swagger(v1/v2)/ Runscope文件. "

起初我已经搞砸了,但没有答案满足我的情况.
所以我的问题是如何创建邮递员需要的文件.顺便说一句,我不熟悉招摇.
JDp*_*war 67
我从事PHP工作,并使用Swagger 2.0来记录API.Swagger文档是动态创建的(至少这是我在PHP中使用的).该文档以JSON格式生成.
样本文件
{
"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": {
}
}
Run Code Online (Sandbox Code Playgroud)
这可以导入Postman,如下所示.
您也可以使用"从链接导入".这里粘贴从Swagger或任何其他API文档工具生成API的JSON格式的URL.
这是我的Document(JSON)生成文件.它是在PHP中.我不知道JAVA和Swagger.
<?php
require("vendor/autoload.php");
$swagger = \Swagger\scan('path_of_the_directory_to_scan');
header('Content-Type: application/json');
echo $swagger;
Run Code Online (Sandbox Code Playgroud)
Mik*_*Mik 54
使用 .Net Core 现在很容易:
Usa*_*jad 10
接受的答案是正确的,但我将重写java.
我目前正在使用Swagger V2with Spring Boot 2,这是一个简单的 3 步过程。
步骤 1:在pom.xml文件中添加所需的依赖项。第二个依赖项是可选的,只有在需要时才使用它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>
Run Code Online (Sandbox Code Playgroud)
第二步:添加配置类
@Configuration
@EnableSwagger2
public class SwaggerConfig {
public static final Contact DEFAULT_CONTACT = new Contact("Usama Amjad", "/sf/users/329315731/", "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);
}
}
Run Code Online (Sandbox Code Playgroud)
第 3 步:设置完成,现在您需要将 API 记录在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
}
Run Code Online (Sandbox Code Playgroud)
用法:
您可以访问您的文档,http://localhost:8080/v2/api-docs只需将其复制并粘贴到 Postman 中即可导入收藏。
可选的 Swagger UI:您还可以使用独立的 UI,无需任何其他休息客户端http://localhost:8080/swagger-ui.html,这非常好,您可以轻松托管您的文档。
| 归档时间: |
|
| 查看次数: |
46131 次 |
| 最近记录: |