当前位置:   article > 正文

swagger扩展,默认plugin删除替换,自定义plugin扩展_operationbuilderplugin

operationbuilderplugin

一、删除默认plugin

  1. @Configuration
  2. @EnableSwagger2
  3. public class SwaggerConfig {
  4. @Bean
  5. public Docket api() {
  6. TypeResolver resolver = new TypeResolver();
  7. AlternateTypeRule timestampRule = new AlternateTypeRule(resolver.resolve(Timestamp.class), resolver.resolve(Object.class));
  8. Docket docket = new Docket(DocumentationType.SWAGGER_2)
  9. .select()
  10. .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) //只显示添加@Api注解的类
  11. .build()
  12. .alternateTypeRules(timestampRule)
  13. .apiInfo(apiInfo());
  14. removeDefaultPlugin();
  15. return docket;
  16. }
  17. private ApiInfo apiInfo() {
  18. return new ApiInfoBuilder()
  19. .title("xxxxAPI") // 粗标题
  20. .build();
  21. }
  22. /** 去掉默认的PluginRegistry插件 */
  23. @SuppressWarnings("unchecked")
  24. private void removeDefaultPlugin() {
  25. // 从spring容器中获取swagger插件注册表
  26. PluginRegistry<OperationBuilderPlugin, DocumentationType> pluginRegistry =
  27. ApplicationContextHelper.getBean("operationBuilderPluginRegistry", PluginRegistry.class);
  28. // 插件集合
  29. List<OperationBuilderPlugin> plugins = pluginRegistry.getPlugins();
  30. // 从spring容器中获取需要删除的插件
  31. OperationParameterReader operationParameterReader = ApplicationContextHelper.getBean(OperationParameterReader.class);
  32. // 原plugins集合不能修改,创建新集合,通过反射替换
  33. if (pluginRegistry.contains(operationParameterReader)) {
  34. List<OperationBuilderPlugin> plugins_new = new ArrayList<OperationBuilderPlugin>(plugins);
  35. plugins_new.remove(operationParameterReader);
  36. try {
  37. Field field = PluginRegistrySupport.class.getDeclaredField("plugins");
  38. field.setAccessible(true);
  39. field.set(pluginRegistry, plugins_new);
  40. } catch (Exception e) {
  41. e.printStackTrace();
  42. }
  43. }
  44. }
  45. }

以上为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。

  1. @Component
  2. @Order(Ordered.HIGHEST_PRECEDENCE)
  3. public class OperationParameterReader2 implements OperationBuilderPlugin {
  4. private final ModelAttributeParameterExpander2 expander;
  5. @Autowired
  6. private DocumentationPluginsManager pluginsManager;
  7. @Autowired
  8. public OperationParameterReader2(ModelAttributeParameterExpander2 expander) {
  9. this.expander = expander;
  10. }
  11. // 省略
  12. }

ModelAttributeParameterExpander2修改expand方法

  1. public List<Parameter> expand(ExpansionContext context) {
  2. List<Parameter> parameters = newArrayList();
  3. Set<String> beanPropNames = getBeanPropertyNames(context.getParamType().getErasedType());
  4. Iterable<ResolvedField> fields = FluentIterable.from(fieldProvider.in(context.getParamType()))
  5. .filter(onlyBeanProperties(beanPropNames));
  6. // 添加判断,只展开ApiModelProperty注解且hidden为false的字段
  7. List<ResolvedField> fields_new = new ArrayList<ResolvedField>();
  8. Iterator<ResolvedField> it = fields.iterator();
  9. while (it.hasNext()) {
  10. ResolvedField resolvedField = it.next();
  11. Field field = resolvedField.getRawMember();
  12. field.setAccessible(true);
  13. ApiModelProperty apiModelProperty = field.getDeclaredAnnotation(ApiModelProperty.class);
  14. if (apiModelProperty!=null && apiModelProperty.hidden()==false) {
  15. fields_new.add(resolvedField);
  16. }
  17. }
  18. // 添加判断结束
  19. LOG.debug("Expanding parameter type: {}", context.getParamType());
  20. AlternateTypeProvider alternateTypeProvider = context.getDocumentationContext().getAlternateTypeProvider();
  21. FluentIterable<ModelAttributeField> modelAttributes = from(fields_new)
  22. .transform(toModelAttributeField(alternateTypeProvider));
  23. FluentIterable<ModelAttributeField> expendables = modelAttributes.filter(not(simpleType()))
  24. .filter(not(recursiveType(context)));
  25. // 省略
  26. return FluentIterable.from(parameters).filter(not(hiddenParameters())).toList();
  27. }

需要注意的是OperationParameterReader和ModelAttributeParameterExpander都有@Component所以我们自定义的类也需要加入spring扫描。

注:这篇博客的扩展方式太粗暴了,如果有兴趣,可以看我另一篇博客

https://blog.csdn.net/qq_35433926/article/details/89514913

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/373110
推荐阅读
相关标签
  

闽ICP备14008679号