最近我用 SpringMvc 和swagger-ui(v2)编写了restful API。我注意到Postman中的导入功能:
所以我的问题是如何创建Postman所需的文件?
我尝试使用谷歌搜索,但没有答案适用于我的情况。
顺便说一句,我不熟悉招摇。
答案 0 :(得分:32)
我使用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,如下所示。
您还可以使用“从链接导入”。这里粘贴从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;
答案 1 :(得分:6)
答案 2 :(得分:1)
可接受的答案是正确的,但我将重写java
的完整步骤。
我目前将Swagger V2
与Spring Boot 2
一起使用,这是一个简单的三步过程。
步骤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步:,设置已完成,现在您需要在controllers
中记录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:您还可以通过http://localhost:8080/swagger-ui.html
使用独立的UI,而无需任何其他Rest Client,这非常好,您可以轻松地托管文档。
答案 3 :(得分:0)
您还可以在线获取一些示例文件,以验证这一点(如果您的swagger文档中有错误)。
答案 4 :(得分:0)
您可以执行以下操作:邮递员->导入->链接-> {root_url}/v2/api-docs
答案 5 :(得分:0)
建议您阅读此答案
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