springfox swagger-ui多个包路径扫描匹配的改造支持

使用springfox中的 RequestHandlerSelectors.basePackage("com.xxx") 只能支持单个包路径的扫描匹配,

如果要想支持多个包路径的匹配我们需要修改springfox里面的代码来支持他,现做以下修改来支持多包路径匹配。

  1. package com.xxx.swagger;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.context.annotation.PropertySource;
  6. import org.springframework.context.annotation.PropertySources;
  7. import springfox.documentation.RequestHandler;
  8. import springfox.documentation.builders.ApiInfoBuilder;
  9. import springfox.documentation.builders.PathSelectors;
  10. import springfox.documentation.service.ApiInfo;
  11. import springfox.documentation.spi.DocumentationType;
  12. import springfox.documentation.spring.web.plugins.Docket;
  13. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  14. import com.google.common.base.Function;
  15. import com.google.common.base.Optional;
  16. import com.google.common.base.Predicate;
  17. /**
  18. * Swagger2 UI配置
  19. *
  20. * <pre>
  21. * 通过访问http://your ip:8090/api/swagger-ui.html查看发布的REST接口;
  22. * </pre>
  23. *
  24. * @author 许畅
  25. * @since JDK1.7
  26. * @version 2017年10月19日 许畅 新建
  27. */
  28. @Configuration
  29. @PropertySources({ @PropertySource(value = "classpath:swagger2.properties", ignoreResourceNotFound = true, encoding = "UTF-8") })
  30. @EnableSwagger2
  31. public class Swagger2UIConfig {
  32. /**
  33. * 获取API标题
  34. */
  35. @Value( "${swagger2.title}")
  36. public String title = "Spring Boot中使用Swagger2构建Restful API";
  37. /**
  38. * Swagger2创建Docket的Bean
  39. *
  40. * @return Docket
  41. */
  42. @Bean
  43. public Docket createRestApi() {
  44. return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
  45. .apis(Swagger2UIConfig.basePackage( "com.xxx1,com.xxx2")).paths(PathSelectors.any()).build();
  46. }
  47. /**
  48. * Predicate that matches RequestHandler with given base package name for the class of the handler method.
  49. * This predicate includes all request handlers matching the provided basePackage
  50. *
  51. * @param basePackage - base package of the classes
  52. * @return this
  53. */
  54. public static Predicate<RequestHandler> basePackage(final String basePackage) {
  55. return new Predicate<RequestHandler>() {
  56. @Override
  57. public boolean apply(RequestHandler input) {
  58. return declaringClass(input).transform(handlerPackage(basePackage)).or( true);
  59. }
  60. };
  61. }
  62. /**
  63. * 处理包路径配置规则,支持多路径扫描匹配以逗号隔开
  64. *
  65. * @param basePackage 扫描包路径
  66. * @return Function
  67. */
  68. private static Function<Class<?>, Boolean> handlerPackage( final String basePackage) {
  69. return new Function<Class<?>, Boolean>() {
  70. @Override
  71. public Boolean apply(Class<?> input) {
  72. for (String strPackage : basePackage.split( ",")) {
  73. boolean isMatch = input.getPackage().getName().startsWith(strPackage);
  74. if (isMatch) {
  75. return true;
  76. }
  77. }
  78. return false;
  79. }
  80. };
  81. }
  82. /**
  83. * @param input RequestHandler
  84. * @return Optional
  85. */
  86. private static Optional<? extends Class<?>> declaringClass(RequestHandler input) {
  87. return Optional.fromNullable(input.declaringClass());
  88. }
  89. /**
  90. * Swagger2创建该Api的基本信息
  91. *
  92. * @return ApiInfo
  93. */
  94. @Bean
  95. public ApiInfo apiInfo() {
  96. return new ApiInfoBuilder().title(title)
  97. .description( "更多Swagger2配置相关文章请关注:https://springfox.github.io/springfox/docs/current/")
  98. .termsOfServiceUrl( "https://springfox.github.io/springfox/docs/current/").version( "1.0").build();
  99. }
  100. }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值