検索

261.3. 例: PetStore

download PDF

examples ディレクトリーの camel-example-rest-swagger プロジェクトの例をチェックアウトします。

たとえば、PetStore が提供する REST API は単に仕様 URI と、Swagger 仕様から必要な操作 ID を参照する場合や、仕様をダウンロードして、自動的に使用されるように CLASSPATH の swagger.json として保存します。Undertow コンポーネントを使用してすべてのリクエストを実行し、Spring Boot に対して Camels excelent サポートを実行してみましょう。

以下は、Maven POM ファイルで定義される依存関係です。

<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-undertow-starter</artifactId>
</dependency>

<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-rest-swagger-starter</artifactId>
</dependency>

Undertow コンポーネントと RestSwaggerComponent を定義して開始します。

@Bean
public Component petstore(CamelContext camelContext, UndertowComponent undertow) {
    RestSwaggerComponent petstore = new RestSwaggerComponent(camelContext);
    petstore.setSpecificationUri("http://petstore.swagger.io/v2/swagger.json");
    petstore.setDelegate(undertow);

    return petstore;
}
注記

Spring Boot の Camel のサポートは UndertowComponent Spring Bean を自動作成し、接頭辞 camel.component.undertow. を使用して application.properties (または application.yml)を使用して設定できます。ここで petstore コンポーネントを定義し、Camel コンテキストに名前付きコンポーネントを定義して、PetStore REST API との対話に使用できます。これが使用する唯一の rest-swagger コンポーネントである場合( 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.