时间:2019-5-23 14:46:50
地点:家中
前言
我们在上一篇文章,通过@EnableSwagger2
注解,知道springfox使用开启Plugin注解的配置,注入了很多Plugin的配置类,结合我们第二篇针对Spring Plugin的使用文章介绍,该篇主要探索springfox中的各种不同Plugin的具体作用,以及声明了那些方法
插件
在SpringfoxWebMvcConfiguration
配置中,主要涉及了以下Plugin
- DocumentationPlugin
- ApiListingBuilderPlugin
- OperationBuilderPlugin
- ParameterBuilderPlugin
- ExpandedParameterBuilderPlugin
- OperationModelsProviderPlugin
- DefaultsProviderPlugin
- PathDecorator
- ApiListingScannerPlugin
代码结构一览:
plugin中声明的接口,都是为处理contexts上下文中的属性
DocumentationPlugin
先来看DocumentationPlugin
的源码
public interface DocumentationPlugin extends Plugin<DocumentationType> {
/**
* @return indicator to determine if the plugin is enabled
*/
boolean isEnabled();
DocumentationType getDocumentationType();
/**
* Creates a documentation context based on a given DocumentationContextBuilder
*
* @param builder - @see springfox.documentation.spi.service.contexts.DocumentationContextBuilder
* @return context to use for building the documentation
*/
DocumentationContext configure(DocumentationContextBuilder builder);
/**
* Gets the group name for the plugin. This is expected to be unique for each instance of the plugin
* @return group the plugin belongs to
*/
String getGroupName();
}
此Plugin使用的分隔符类是DocumentationType
,文档类型,在Springfox中声明了三个版本的文档类型,主要是:
- SWAGGER_2:swagger的2.0版本
- SWAGGER_12:Swagger的1.2版本
- SPRING_WEB:springfox项目是由原spring-mvc-swagger项目演变而来,所以这是最早的一个版本
DocumentationPlugin
定义了三个方法:
- 是否启用
- 获取文档类型
- 通过文档上下文Builder构建文档上下文对象
- 获取分组名称
那么,他的实现类是谁,我们通过IDEA的编辑器功能能轻松定位到,是Docket类
来看类图:
由于Docket对象是最终实现类,而我们开发者一般在使用时,都是通过创建Docket的实体对象来注入到Spring的容器中
所以,我们创建几个Docket对象的Bean实例,那么通过PluginRetry<DocumentationPlugin,DocumentationType>
的getPlugins()方法,最终就会获取到外部注入的Docket对象实例,然后再程序中就可以使用了