【Swagger2】【1】匹配多個controller


前言:項目中有多個模塊,所以有多個controller層。我是用方案二的

正文:

方案一:使用多個controller的共同擁有的父類

@Bean
public Docket createRestApi() {
    return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.xx"))
            .paths(PathSelectors.any())
            .build();
}

方法二:指定所有controller的都實現的一個接口,比如@RestController

@Bean
public Docket createRestApi() {
    return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            .select()
            .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
            .paths(PathSelectors.any())
            .build();
}

錯誤的兩種寫法

@Bean
public Docket createRestApi() {
    return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.xx.*.controller"))
            .paths(PathSelectors.any())
            .build();
}
@Bean
public Docket createRestApi() {
    return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.xx.course.controller"))
            .apis(RequestHandlerSelectors.basePackage("com.xx.user.controller"))
            .paths(PathSelectors.any())
            .build();
}

參考博客:

swagger2 如何匹配多個controller - 賈樹丙 - 博客園
http://www.cnblogs.com/acm-bingzi/p/swagger2-controller.html


免責聲明!

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



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