提交 5f2d42ef 作者: 方治民

feat: 新增 SwaggerDoc 分组配置

上级 182a1b2b
/**
* 公共接口
*
* @author Jim
* @version 0.1
* 2022/3/23 16:18
* 2022/6/22 11:30
*/
package com.yiring.auth;
package com.yiring.app.web.common;
/* (C) 2021 YiRing, Inc. */
package com.yiring.app.web;
package com.yiring.app.web.example;
import cn.hutool.extra.spring.SpringUtil;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
......@@ -26,6 +26,11 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 示例接口
* @author Jim
*/
@Slf4j
@Validated
@SuppressWarnings({ "deprecation" })
......
/**
* Controller
*
* @author Jim
* @version 0.1
* 2022/3/24 16:37
* 2022/6/22 11:29
*/
package com.yiring.auth.web;
package com.yiring.app.web;
......@@ -20,6 +20,7 @@ import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Profile;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
......@@ -58,13 +59,34 @@ public class SwaggerConfig implements CommandLineRunner {
@Resource
OpenApiExtensionResolver openApiExtensionResolver;
@Bean
@Bean(name = "api.default")
public Docket api() {
return api(PathSelectors.any());
return api("@default", List.of(""), PathSelectors.any());
}
private Docket api(Predicate<String> paths) {
String group = "default";
@Bean(name = "api.auth")
public Docket auth() {
return api("Auth", List.of("com.yiring.auth.web.auth"), PathSelectors.any());
}
@Bean(name = "api.common")
public Docket common() {
return api("公共", List.of("com.yiring.common.web", "com.yiring.app.web.common"), PathSelectors.any());
}
@Bean(name = "api.example")
public Docket example() {
return api("示例", List.of("com.yiring.app.web.example"), PathSelectors.any());
}
private Docket api(String group, List<String> basePackages, Predicate<String> paths) {
// 扫描多个包
Predicate<RequestHandler> predicate = basePackages
.stream()
.map(RequestHandlerSelectors::basePackage)
.reduce(Predicate::or)
.orElse(RequestHandlerSelectors.none());
return new Docket(DocumentationType.SWAGGER_2)
.groupName(group)
.apiInfo(apiInfo())
......@@ -74,7 +96,7 @@ public class SwaggerConfig implements CommandLineRunner {
.globalResponseMessage(RequestMethod.DELETE, buildGlobalResponseMessage())
.globalResponseMessage(RequestMethod.PUT, buildGlobalResponseMessage())
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class).and(predicate))
.paths(paths)
.build()
.extensions(openApiExtensionResolver.buildExtensions(group));
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论