Swagger入门

概述

swagger是一种易用而强大的API开发工具套件(suite),从设计、编写文档到测试和部署(即整个API生命周期),它能为个人、团队提供支持。

Swagger is a powerful yet easy-to-use suite of API developer tools for teams and individuals, enabling development across the entire API lifecycle, from design and documentation, to test and deployment.

Swagger consists of a mix of open source, free and commercially available tools that allow anyone, from technical engineers to street smart product managers to build amazing APIs that everyone loves.

Swagger is built by SmartBear Software, the leader in software quality tools for teams. SmartBear is behind some of the biggest names in the software space, including Swagger, SoapUI and QAComplete .原文

swagger-ui效果

优势

API DX(developer Experience)很重要,用swagger辅助可帮助你更好的设计、编写、测试和部署API。

API DX重要性体现在:

  • 文档(documentation)在DX中的角色。好的API文档的编写需要技巧性(代码示例等),一个设计良好的API文档会帮助你更好的消费它,把它集成在你的系统里。
  • 良好的文档不是一个简单的任务。文档是冗长耗时的(版本更新后,API文档需要随之更新)

原文

API设计最佳实践

良好API设计的特点:
  • 易于阅读和使用。
  • 混用困难。即理解难以偏差。
  • 完整性和精确性。
集合(Collections),资源(Resources)和他们的URLs
理解集合和资源

资源是REST概念的基础。一个资源是一个对象,资源有数据,与其它资源构成关系。

名词用于描述URLs更佳

The base URL should be neat, elegant, and simple so that developers using your product can easily use them in their web applications.

用HTTP方法来描述资源的功能
MethodDescription
GETUsed to retrieve a representation of a resource.
POSTUsed to create new new resources and sub-resources
PUTUsed to update existing resources
PATCHUsed to update existing resources
DELETEUsed to delete existing resources
返回(响应)Responses
用反馈帮助开发者成功
给所有GET响应一个例子
请求Request
优雅地处理复杂事情

原文

API注解标签

原文

Quick Annotation Overview

NameDescription
@ApiMarks a class as a Swagger resource.
@ApiImplicitParamRepresents a single parameter in an API Operation.
@ApiImplicitParamsA wrapper to allow a list of multiple ApiImplicitParam objects.
@ApiModelProvides additional information about Swagger models.
@ApiModelPropertyAdds and manipulates data of a model property.
@ApiOperationDescribes an operation or typically a HTTP method against a specific path.
@ApiParamAdds additional meta-data for operation parameters.
@ApiResponseDescribes a possible response of an operation.
@ApiResponsesA wrapper to allow a list of multiple ApiResponse objects.
@AuthorizationDeclares an authorization scheme to be used on a resource or an operation.
@AuthorizationScopeDescribes an OAuth2 authorization scope.
@ResponseHeaderRepresents a header that can be provided as part of the response.

参考文档

swagger官网
swagger-maven-plugin - kongchen

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Swagger是一种用于设计、构建、文档化和使用RESTful Web服务的开源工具集。它提供了一种简单且强大的方式来描述API,以及生成交互式文档、客户端SDK和服务端存根代码。下面是关于Swagger入门到精通的步骤: 1. 安装Swagger:首先,你需要安装Swagger工具集。你可以从Swagger官方网站或者通过包管理工具(如npm、pip等)来安装Swagger。 2. 创建Swagger文档:一旦安装完成,你可以开始创建Swagger文档。Swagger使用YAML或JSON格式来描述API。你可以通过编辑器(如Swagger Editor)或者直接编写YAML/JSON文件来创建文档。 3. 定义API:在Swagger文档中,你需要定义API的各个方面,包括路径、HTTP方法、请求参数、响应数据等。你可以使用Swagger提供的规范来定义API的各个部分。 4. 添加元数据:除了API定义,你还可以添加一些元数据,如API的标题、描述、版本号等。这些信息将在生成的文档中显示,并帮助用户更好地理解和使用你的API。 5. 生成文档:一旦你完成了Swagger文档的编写,你可以使用Swagger工具集中的工具来生成交互式文档。这些文档将提供给开发人员和用户,以便他们了解和使用你的API。 6. 测试API:Swagger还提供了一些工具来测试API。你可以使用Swagger UI来发送请求并查看响应数据。这样可以确保你的API按照预期工作,并帮助你发现和解决潜在的问题。 7. 生成客户端SDK和服务端存根代码:Swagger还可以根据API定义生成客户端SDK和服务端存根代码。这些代码将帮助开发人员更轻松地集成和使用你的API。 8. 部署和使用API:最后,你需要将API部署到服务器上,并让用户使用它。你可以将生成的文档和代码提供给用户,以便他们能够快速上手并开始使用你的API。 通过以上步骤,你可以从入门到精通地使用Swagger来设计、构建、文档化和使用RESTful Web服务。记得不断学习和实践,掌握更多高级功能和最佳实践,以提升你的Swagger技能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值