第280章 例: ペットストア
examples
ディレクトリーにある camel-example-rest-openapi
プロジェクトの例を確認してください。
たとえば、PetStore が提供する REST API を使用する場合は、OpenApi 仕様から仕様 URI と目的の操作 ID を参照するか、仕様をダウンロードして、自動的に使用されるように CLASSPATH の (ルートにある) openapi.json
として保存します。.Undertow コンポーネントを使用してすべてのリクエストを実行し、Camels は Spring Boot の優れたサポートを提供します。
Maven POM ファイルで定義されている依存関係は次のとおりです。
<dependency> <groupId>org.apache.camel.springboot</groupId> <artifactId>camel-undertow-starter</artifactId> </dependency> <dependency> <groupId>org.apache.camel.springboot</groupId> <artifactId>camel-rest-openapi-starter</artifactId> </dependency>
Undertow コンポーネントと RestOpenApiComponent を定義することから始めます。
@Bean public Component petstore(CamelContext camelContext, UndertowComponent undertow) { RestOpenApiComponent petstore = new RestOpenApiComponent(camelContext); petstore.setSpecificationUri("https://petstore3.swagger.io/api/v3/openapi.json"); petstore.setDelegate(undertow); return petstore; }
Camel での Spring Boot のサポートにより、UndertowComponent
Spring Bean が自動作成され、接頭辞 camel.component.undertow
を使用して application.properties
(または application.yml
) を使用して設定できます。.ここで petstore
コンポーネントを定義しているのは、PetStore REST API と対話するために使用できる名前付きコンポーネントを Camel コンテキストに含めるためです。これが使用される唯一の rest-openapi
コンポーネントである場合は、同じ方法で設定できます (application.properties
)。
これで、アプリケーションで ProducerTemplate
を使用して PetStore REST メソッドを呼び出すことができます。
@Autowired ProducerTemplate template; String getPetJsonById(int petId) { return template.requestBodyAndHeaders("petstore:getPetById", null, "petId", petId); }