280.4. 例: ペットストア

examples ディレクトリーにある camel-example-rest-swagger プロジェクトの例を確認してください。

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

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;
}
注記

Camel での Spring Boot のサポートにより、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);
}