springfox 源码分析(十三) 自定义扩展实现接口的排序

本文详细分析了如何在Springfox Swagger中实现接口排序的自定义扩展。通过扩展Swagger的根对象和path接口,遵循扩展属性的命名规则,并利用Springfox的Plugin系统,实现了接口的排序功能。开发者可以通过添加特定的扩展属性(如`x-order`),并在前端展示时依据该属性排序接口,从而提高接口文档的易读性和使用效率。
摘要由CSDN通过智能技术生成

很多时候,Swagger定义的标准并不能满足我们实际的需求,比如拿分组后的接口来说,有适合我们希望我们的接口能够排序,假如我们当前有一个注册的需求实现,那么他的接口可能是这样的:

1.获取验证码 -> 2.校验用户名是否有效 -> 3.注册验证 -> 4.登录

如果我们没有排序的情况下,上面的接口对于开发人员来说可能是杂乱无章的,对于初级的接口对接人员来说,排序更能让开发者把当前的需求清晰明了的用代码来实现掉,为此,接口文档的作用也能最大化.

那么,在swagger的标准中,那些允许我们自定义扩展,在Springfox中我们又如何来实现我们的自定义扩展呢?

Swagger标准

先来看Swagger定义的几个标准属性,可参考官方文档

swagger string Required. Specifies the Swagger Specification version being used. It can be used by the Swagger UI and other clients to interpret the API listing. The value MUST be "2.0".
info Info Object Required. Provides metadata about the API. The metadata can be used by the clients if needed.
host string The host (name or ip) serving the API. This MUST be the host only and does not include the scheme nor sub-paths. It MAY include a port. If the host is not included, the host serving the documentation is to be used (including the port). The host does not support path templating.
basePath string The base path on which the API is served, which is relative to the host. If it is not included, the API is served directly under the host. The value MUST start with a leading slash (/). The basePath does not support path templating.
schemes [string] The transfer protocol of the API. Values MUST be from the list: "http""https""ws""wss". If the schemes is not included, the default scheme to be used is the one used to access the Swagger definition itself.
consumes [string] A list of MIME types the APIs can consume. This is global to all APIs but can be overridden on specific API calls. Value MUST be as described under Mime Types.
produces [string] A list of MIME types the APIs can produce. This is global to all APIs but can be overridden on specific API calls. Value MUST be as described under Mime Types.
paths Paths Object Required. The available paths and operations for the API.
definitions Definitions Object An object to hold data types produced and consumed by operations.
parameters Parameters Definitions Object An object to hold parameters that can be used across operations. This property does not define global parameters for all operations.
responses Responses Definitions Object An object to hold responses that can be used across operations. This property does not define global responses for all operations.
securityDefinitions Security Definitions Object Security scheme definitions that can be used across the specification.
security [Security Requirement Object] A declaration of which security schemes are applied for the API as a whole. The list of values describes alternative security schemes that can be used (that is, there is a logical OR between the security requirements). Individual operations can override this definition.
tags [Tag Object] A list of tags used by the specification with additional metadata. The order of the tags can be used to reflect on their order by the parsing tools. Not all tags that are used by the Operation
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值