最近我用SpringMvc和swagger-ui(v2)编写了restful api。我注意到Postman中的Import函数:
所以我的问题是如何创建邮差需要的文件?
我不熟悉斯威格。
最近我用SpringMvc和swagger-ui(v2)编写了restful api。我注意到Postman中的Import函数:
所以我的问题是如何创建邮差需要的文件?
我不熟悉斯威格。
我使用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": {
}
}
这可以导入到邮差如下。
点击Postman UI左上角的“Import”按钮。 您将看到用于导入API文档的多个选项。点击“粘贴原始文本”。 将JSON格式粘贴到文本区,然后单击导入。 你会看到你所有的api为“邮差收集”,并可以从邮差使用它。
你也可以使用“Import From Link”。在这里粘贴URL,从Swagger或任何其他API文档工具生成JSON格式的API。
这是我的文档(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;
点击橙色按钮(“选择文件”) 浏览到Swagger文档(Swagger .yaml) 选择文件后,在POSTMAN中创建一个新的集合。它将包含基于端点的文件夹。
你也可以在网上得到一些样本swagger文件来验证这一点(如果你的swagger文档中有错误)。
接受的答案是正确的,但我将为java重写完整的步骤。
我目前使用Swagger V2与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>
步骤2:添加配置类
@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);
}
}
步骤3:设置完成,现在需要在控制器中记录api
@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
}
用法:
您可以从http://localhost:8080/v2/api-docs访问您的文档,只需复制并粘贴在邮差导入集合。
可选Swagger UI:你也可以使用独立的UI,没有任何其他rest客户端通过http://localhost:8080/swagger-ui.html,这是非常好的,你可以托管你的文档没有任何麻烦。
现在使用。net Core非常简单:
你可以在swagger页面上找到JSON URL:
单击该链接并复制URL 现在转到邮差,点击导入:
选择你需要的,你最终会得到一个不错的端点集合:
推荐你看一下这个答案
https://stackoverflow.com/a/51072071/4712855
参考https://stackoverflow.com/posts/39072519的答案,然后部分删除返回的内容。最后,发现swagger缺少一些配置,无法导入postmat。
需要在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.
}
简而言之,ApiInfoBuilder类中的属性被尽可能多地赋值
spring-boot版本:2.3.10.RELEASE Springfox-swagger版本:2.9.2
这是Swagger编辑器界面中对我有用的:
方法1
将YAML文件内容复制到Raw Text区域:
方法二(多步骤)
步骤1:将文件导出为JSON
步骤2:使用Postman“Import”导入JSON文件