2.100. REST OpenApi
使用 OpenAPI 规格作为合同调用 REST 服务。
2.100.1. 什么是内部
-
REST OpenApi 组件, URI 语法:
rest-openapi:specificationUri#operationId
有关用法和配置详情,请参阅上述链接。
2.100.2. Maven 协调
在 code.quarkus.redhat.com 上使用此扩展创建新项目
或者将协调添加到现有项目中:
<dependency> <groupId>org.apache.camel.quarkus</groupId> <artifactId>camel-quarkus-rest-openapi</artifactId> </dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-rest-openapi</artifactId>
</dependency>
2.100.3. 使用方法
2.100.3.1. 所需的依赖项
在使用 rest-openapi 扩展时,必须有一个 RestProducerFactory
实现。当前已知的扩展有:
- camel-quarkus-http
- camel-quarkus-netty-http
Maven 用户需要将这些依赖项之一添加到其 pom.xml
中,例如:
<dependency> <groupId>org.apache.camel.quarkus</groupId> <artifactId>camel-quarkus-http</artifactId> </dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-http</artifactId>
</dependency>
根据用来加载 OpenApi 规格的机制,可能需要额外的依赖项。在使用 file
资源 locator 时,必须将 org.apache.camel.quarkus:camel-quarkus-file
扩展添加为项目依赖项。当使用 ref
或 bean
加载规格时,不仅必须添加 org.apache.camel.quarkus:camel-quarkus-bean
依赖项,但 bean 本身必须使用 @RegisterForReflection
注解。
当将 classpath
资源 locator 与原生代码一起使用时,必须在 application.properties
文件的 quarkus.native.resources.includes
属性中指定到 OpenAPI 规格的路径。例如:
quarkus.native.resources.includes=openapi.json
quarkus.native.resources.includes=openapi.json
2.100.3.2. 首次开发合同
模型类生成已与 quarkus-maven-plugin
集成。因此,不需要使用 swagger-codegen-maven-plugin
,而是将您的合同文件放在 src/main/openapi
中,带有 .json
后缀。并将 generate-code
目标添加到 quarkus-maven-plugin
中,如下所示:
<plugin> <groupId>io.quarkus</groupId> <artifactId>quarkus-maven-plugin</artifactId> <executions> <execution> <goals> <goal>generate-code</goal> </goals> </execution> </executions> </plugin>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>generate-code</goal>
</goals>
</execution>
</executions>
</plugin>
它需要模型类的特定软件包名称,方法是使用 application.properties
文件的 quarkus.camel.openapi.codegen.model-package
属性。例如:
quarkus.camel.openapi.codegen.model-package=org.acme
quarkus.camel.openapi.codegen.model-package=org.acme
此软件包名称也应该添加到 camel.rest.bindingPackageScan
中。
src/main/openapi
中的合同文件需要在 classpath 中添加,因为它们可以在 Camel Rest DSL 中使用。因此您可以在 pom.xml
中添加 src/main/openapi
<build> <resources> <resource> <directory>src/main/openapi</directory> </resource> <resource> <directory>src/main/resources</directory> </resource> </resources> </build>
<build>
<resources>
<resource>
<directory>src/main/openapi</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
在原生模式下运行时,必须指定 quarkus.native.resources.include
等合同文件
quarkus.native.resources.includes=contract.json
quarkus.native.resources.includes=contract.json
2.100.4. 其他 Camel Quarkus 配置
配置属性 | 类型 | default |
---|---|---|
如果为 |
|
|
用于生成的模型类的软件包。 |
|
|
要生成的以逗号分隔的模型列表。所有模型都是默认设置。 |
| |
如果为 |
|
|
如果为 |
|
|
在构建时修复的配置属性。所有其他配置属性可在运行时覆盖。