如何将多个控制器的包基础设置为我的Swagger配置?

huangapple go评论74阅读模式
英文:

How to set multi controller's package base to my SwaggerConfiguration?

问题

我的依赖......

<!-- SPRING BOOT -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>


当我尝试在Swagger中使用两个基础包时,出现了问题。在下面,您可以看到我所指的基础包,而在上面,您可以看到我的依赖项。我尝试使用两个基础包的代码如下。

aPackageBase: com.companyx
bPackageBase: br.com.companyz

----

## 我无法做的事情
* 我无法更改基础包,因为它们是庞大的API。
* 我有超过10个遵循这两个基础包的API。
* 我想要使用 `aPackageBase` 和 `bPackageBase`。
----

## 目前不起作用的代码...

...
String aPackageBase = "com.companyx";
String bPackageBase = "br.com.companyz";

Docket docket = new Docket(DocumentationType.OAS_30).select() //
.apis(RequestHandlerSelectors.basePackage(aPackageBase) //
.and(RequestHandlerSelectors.basePackage(bPackageBase))) // 这不起作用 :(....
.paths(PathSelectors.any()).build()

...

----

## 我想要的是......
我只想使用**两个基础包**。我并不是在尝试在运行时更改它,当然我可以在运行时更改它并且只使用 `一个 packageBase`,但是我已经厌倦了为每个API更改它。如果我可以有 `aPackageBase` 和 `bPackageBase`,它将解救我的生活。哈哈。

---
注意:我认为我不需要展示有关我的Swagger配置的更多信息,因为我的问题仅在于该部分。
英文:

My dependency....

&lt;!-- SPRING BOOT --&gt;
&lt;dependency&gt;
	&lt;groupId&gt;io.springfox&lt;/groupId&gt;
	&lt;artifactId&gt;springfox-boot-starter&lt;/artifactId&gt;
	&lt;version&gt;3.0.0&lt;/version&gt;
&lt;/dependency&gt;


My problem happen when i tried to use in swagger two packages base. Below you can see what i mean with package base and above you can see my dependency. My code that i tried to use two package base are more below.

aPackageBase: com.companyx
bPackageBase: br.com.companyz

Things that I cannot do

  • I cannot change the package base because they are huge APIs.
  • I have more then 10 APIs that follow that 2 package base.
  • I would like to use aPackageBase and bPackageBase.

Current code, that is not working...

...
String aPackageBase = &quot;com.companyx&quot;;
String bPackageBase = &quot;br.com.companyz&quot;;

Docket docket = new Docket(DocumentationType.OAS_30).select() //
	.apis(RequestHandlerSelectors.basePackage(aPackageBase) //
		.and(RequestHandlerSelectors.basePackage(bPackageBase))) // That doesn&#39;t work :(....
		.paths(PathSelectors.any()).build()

...

What would like to have...

I just want to use both packages base. I am not trying to change the at runtime, of course I can change it at runtime and use only one packageBase, but I am tired to change it for each API. If I can have aPackageBase and bPackageBase it will save my life. haaha.


Note: I don't think I need show more about my SwaggerConfiguration because my problem is only in that part.

答案1

得分: 8

你需要稍微修改你的 Docket bean 定义。

Docket docket = new Docket(DocumentationType.OAS_30)
    .select() //
    .apis(RequestHandlerSelectors.basePackage(aPackageBase) //
        .or(RequestHandlerSelectors.basePackage(bPackageBase))) // 这里使用 or,而不是 and
    .paths(PathSelectors.any())
    .build();

RequestHandlerSelectors.basePackage(...) 返回一个 java.util.function.Predicate 对象。

Predicate#and 要求两个条件都为真,但实际上不是这样,因为任何 API 只会存在于其中一个包中。因此使用 Predicate#or

英文:

You need to change your Docket bean definition a little bit.

Docket docket = new Docket(DocumentationType.OAS_30)
    .select() //
    .apis(RequestHandlerSelectors.basePackage(aPackageBase) //
        .or(RequestHandlerSelectors.basePackage(bPackageBase))) // use or here, not and
    .paths(PathSelectors.any())
    .build();

RequestHandlerSelectors.basePackage(...) returns a java.util.function.Predicate object.

Predicate#and requires both condition to be true, which is not, since any API will exist in one of the packages only. Hence Predicate#or.

答案2

得分: 2

Pratapi's answer 是正确的。但是如果你使用的是较旧版本的 springfox,basePackage 会返回一个 Google Guava 谓词([链接](https://springfox.github.io/springfox/javadoc/2.7.0/springfox/documentation/builders/RequestHandlerSelectors.html#basePackage(java.lang.String))。

请注意,Google Guava 谓词扩展了 Java 谓词,并且 or 方法在 Guava 谓词中没有被覆盖。因此,调用 or 将会返回一个 Java 谓词。你可以使用方法引用(或者 lambda 表达式)将一个 Guava 谓词传递给 apis 方法。

//A java.util.function.Predicate
Predicate&lt;RequestHandler&gt; predicate = RequestHandlerSelectors.basePackage(aPackageBase)
          .or(RequestHandlerSelectors.basePackage(bPackageBase));

return docket.select()
           .apis(predicate::test) //.apis(r -&gt; p1.test(r))
           ....
英文:

Pratapi's answer is the right one. But if you are using an older version of springfox, the basePackage returns a Google Guava predicate (link).

Note that the Google Guava predicate extends the Java predicate and the or method is not overriden in the Guava Predicate. Hence, calling or will return a Java predicate. You can use a method reference (or a lambda expression) to pass a Guava predicate to the apis method.

//A java.util.function.Predicate
Predicate&lt;RequestHandler&gt; predicate = RequestHandlerSelectors.basePackage(aPackageBase)
          .or(RequestHandlerSelectors.basePackage(bPackageBase));

return docket.select()
           .apis(predicate::test) //.apis(r -&gt; p1.test(r))
           ....

答案3

得分: 0

以下代码片段适用于Swagger2:

List<Predicate<RequestHandler>> predicateList = new ArrayList<>();
predicateList.add(RequestHandlerSelectors.basePackage(com.sample.one));
predicateList.add(RequestHandlerSelectors.basePackage(com.sample.two));

Docket d = new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(Predicates.or(predicateList))
            .paths(PathSelectors.any())
            .build().forCodeGeneration(true)
            .securitySchemes(Arrays.asList(oAuthSecurityScheme()));
英文:

Below code snippet worked for me for swagger2:

List&lt;Predicate&lt;RequestHandler&gt;&gt; predicateList = new ArrayList&lt;&gt;();
predicateList.add(RequestHandlerSelectors.basePackage(com.sample.one));
predicateList.add(RequestHandlerSelectors.basePackage(com.sample.two));

Docket d = new Docket(DocumentationType.SWAGGER_2)
				.select()
				.apis(Predicates.or(predicateList))
				.paths(PathSelectors.any())
				.build().forCodeGeneration(true)
				.securitySchemes(Arrays.asList(oAuthSecurityScheme()));

huangapple
  • 本文由 发表于 2020年9月5日 03:33:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63747081.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定