261.3. 例: PetStore
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); }