検索

第280章 例: ペットストア

download PDF

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);
}
Red Hat logoGithubRedditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

© 2024 Red Hat, Inc.