赞
踩
一、删除默认plugin
- @Configuration
- @EnableSwagger2
- public class SwaggerConfig {
-
- @Bean
- public Docket api() {
- TypeResolver resolver = new TypeResolver();
- AlternateTypeRule timestampRule = new AlternateTypeRule(resolver.resolve(Timestamp.class), resolver.resolve(Object.class));
- Docket docket = new Docket(DocumentationType.SWAGGER_2)
- .select()
- .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) //只显示添加@Api注解的类
- .build()
- .alternateTypeRules(timestampRule)
- .apiInfo(apiInfo());
- removeDefaultPlugin();
- return docket;
- }
-
- private ApiInfo apiInfo() {
- return new ApiInfoBuilder()
- .title("xxxxAPI") // 粗标题
- .build();
- }
-
- /** 去掉默认的PluginRegistry插件 */
- @SuppressWarnings("unchecked")
- private void removeDefaultPlugin() {
- // 从spring容器中获取swagger插件注册表
- PluginRegistry<OperationBuilderPlugin, DocumentationType> pluginRegistry =
- ApplicationContextHelper.getBean("operationBuilderPluginRegistry", PluginRegistry.class);
- // 插件集合
- List<OperationBuilderPlugin> plugins = pluginRegistry.getPlugins();
- // 从spring容器中获取需要删除的插件
- OperationParameterReader operationParameterReader = ApplicationContextHelper.getBean(OperationParameterReader.class);
- // 原plugins集合不能修改,创建新集合,通过反射替换
- if (pluginRegistry.contains(operationParameterReader)) {
- List<OperationBuilderPlugin> plugins_new = new ArrayList<OperationBuilderPlugin>(plugins);
- plugins_new.remove(operationParameterReader);
- try {
- Field field = PluginRegistrySupport.class.getDeclaredField("plugins");
- field.setAccessible(true);
- field.set(pluginRegistry, plugins_new);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
-
- }
以上为swagger配置类,ApplicationContextHelper是spring容器gong工具类,需要自行实现,以operationBuilderPluginRegistry为例,删除默认OperationParameterReader。
二、自定义plugin扩展
网上比较多的就是参数展开的问题,以下是我的解决方案,请根据自己的情况修改
上一步已经将默认的OperationParameterReader插件移除了,自定义OperationParameterReader2和ModelAttributeParameterExpander2类,分别将springfox.documentation.spring.web.readers.operation.OperationParameterReader和springfox.documentation.spring.web.readers.parameter.ModelAttributeParameterExpander的源码拷进来
OperationParameterReader2修改,将private final ModelAttributeParameterExpander expander;改成private final ModelAttributeParameterExpander2 expander;调用自定义的ModelAttributeParameterExpander2。
- @Component
- @Order(Ordered.HIGHEST_PRECEDENCE)
- public class OperationParameterReader2 implements OperationBuilderPlugin {
- private final ModelAttributeParameterExpander2 expander;
-
- @Autowired
- private DocumentationPluginsManager pluginsManager;
-
- @Autowired
- public OperationParameterReader2(ModelAttributeParameterExpander2 expander) {
- this.expander = expander;
- }
- // 省略
- }
ModelAttributeParameterExpander2修改expand方法
- public List<Parameter> expand(ExpansionContext context) {
-
- List<Parameter> parameters = newArrayList();
- Set<String> beanPropNames = getBeanPropertyNames(context.getParamType().getErasedType());
- Iterable<ResolvedField> fields = FluentIterable.from(fieldProvider.in(context.getParamType()))
- .filter(onlyBeanProperties(beanPropNames));
-
- // 添加判断,只展开ApiModelProperty注解且hidden为false的字段
- List<ResolvedField> fields_new = new ArrayList<ResolvedField>();
- Iterator<ResolvedField> it = fields.iterator();
- while (it.hasNext()) {
- ResolvedField resolvedField = it.next();
- Field field = resolvedField.getRawMember();
- field.setAccessible(true);
- ApiModelProperty apiModelProperty = field.getDeclaredAnnotation(ApiModelProperty.class);
- if (apiModelProperty!=null && apiModelProperty.hidden()==false) {
- fields_new.add(resolvedField);
- }
- }
- // 添加判断结束
-
- LOG.debug("Expanding parameter type: {}", context.getParamType());
- AlternateTypeProvider alternateTypeProvider = context.getDocumentationContext().getAlternateTypeProvider();
-
- FluentIterable<ModelAttributeField> modelAttributes = from(fields_new)
- .transform(toModelAttributeField(alternateTypeProvider));
-
- FluentIterable<ModelAttributeField> expendables = modelAttributes.filter(not(simpleType()))
- .filter(not(recursiveType(context)));
- // 省略
- return FluentIterable.from(parameters).filter(not(hiddenParameters())).toList();
- }
需要注意的是OperationParameterReader和ModelAttributeParameterExpander都有@Component所以我们自定义的类也需要加入spring扫描。
注:这篇博客的扩展方式太粗暴了,如果有兴趣,可以看我另一篇博客
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。