最近我用SpringMvc和swagger-ui(v2)编写了restful api。我注意到Postman中的Import函数:
所以我的问题是如何创建邮差需要的文件?
我不熟悉斯威格。
最近我用SpringMvc和swagger-ui(v2)编写了restful api。我注意到Postman中的Import函数:
所以我的问题是如何创建邮差需要的文件?
我不熟悉斯威格。
当前回答
你可以这样做:邮差->导入->链接-> {root_url}/v2/api-docs
其他回答
现在使用。net Core非常简单:
你可以在swagger页面上找到JSON URL:
单击该链接并复制URL 现在转到邮差,点击导入:
选择你需要的,你最终会得到一个不错的端点集合:
接受的答案是正确的,但我将为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,这是非常好的,你可以托管你的文档没有任何麻烦。
点击橙色按钮(“选择文件”) 浏览到Swagger文档(Swagger .yaml) 选择文件后,在POSTMAN中创建一个新的集合。它将包含基于端点的文件夹。
你也可以在网上得到一些样本swagger文件来验证这一点(如果你的swagger文档中有错误)。
推荐你看一下这个答案
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文件