(三)Swagger配置多項目共用


重構了多個項目后,在聯調接口時,查看api會發現Swagger在幾個項目可用,有幾個不可用,配置都一樣,掃描也充分,那問題出在哪里呢?先仔細找了下Docket的源碼,發現有這么個方法:

/**
   * Predicate that matches RequestHandler with given base package name for the class of the handler method.
   * This predicate includes all request handlers matching the provided basePackage
   *
   * @param basePackage - base package of the classes
   * @return this
   */
  public static Predicate<RequestHandler> basePackage(final String basePackage) {
    return new Predicate<RequestHandler>() {
      @Override
      public boolean apply(RequestHandler input) {
        return declaringClass(input).transform(handlerPackage(basePackage)).or(true);
      }
    };
  }

而我們在配置中,就用到這個方法:

return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title("some title")
                        .description("some description")
                        .termsOfServiceUrl("this is url")
                        .version("0.01")
                        .build())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.***.qaqc"))
                .paths(PathSelectors.any())
                .build();

由於整合了多個項目,每個項目的包命名規則不一致掃描的包的規則沒有完全匹配所有項目,導致有些可以訪問Swagger有些不能訪問,問題迎刃而解。


項目中是用JWT作為整個spring cloud項目的單點登錄Token,如何讓Swagger也可以支持,讓我們在每次mock中都添加Authentication呢?遵循上面解決問題的思路,我們又找到了這個方法:

/**
   * Adds default parameters which will be applied to all operations.
   *
   * @param operationParameters parameters which will be globally applied to all operations
   * @return this Docket
   */
  public Docket globalOperationParameters(List<Parameter> operationParameters) {
    this.globalOperationParameters.addAll(nullToEmptyList(operationParameters));
    return this;
  }

只要按照方法添加合理的參數,功能就可以實現:

ParameterBuilder authParam = new ParameterBuilder();
List<Parameter> paramList = new ArrayList<>();
authParam.name(authHeader).description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false);
paramList.add(authParam.build());
docket.globalOperationParameters(paramList);

但還有部分項目是不需要做token校驗的,也要保證其兼容性,那么我們可以稍微改造一下:

/**
 * @author zhangqiuyang
 * Created on 2018/4/21.
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Value("${system.auth.header:null}")
    private String authHeader;

    /**
     * 初始化api
     *
     * @return Docket
     */
    @Bean
    public Docket createRestApi() {
        List<Parameter> paramList = null;

        if (!"null".equals(authHeader)) {
            paramList = new ArrayList<>();
            ParameterBuilder authParam = new ParameterBuilder();
            authParam.name(authHeader).description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false);
            paramList.add(authParam.build());
        }
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title("**************")
                        .description("************")
                        .termsOfServiceUrl("http://www.komect.com/")
                        .version("0.01")
                        .build())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.cmhi.qaqc"))
                .paths(PathSelectors.any())
                .build();
        if (paramList != null) {
            docket.globalOperationParameters(paramList);
        }
        return docket;
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM