280.4. 예: PetStore

examples 디렉토리의 camel-example-rest-swagger 프로젝트에서 예제를 체크아웃합니다.

예를 들어, PetStore 가 제공된 REST API를 사용하여 Swagger 사양에서 사양과 원하는 작업 ID를 참조하거나 사양을 다운로드하여 CLASSPATH의 swagger.json (root)으로 자동 사용되는 방식으로 저장할 수 있습니다. 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;
}
참고

Camel for Spring Boot의 지원은 UndertowComponent Spring bean을 자동으로 생성하고 접두사 camel.component.undertow를 사용하여 application.properties (또는 application.yml)를 사용하여 구성할 수 있습니다. PetStore REST API와 상호 작용하는 데 사용할 수 있는 Camel 컨텍스트에 이름이 지정된 구성 요소를 사용하기 위해 여기에서 petstore 구성 요소를 정의합니다. 이 구성 요소는 동일한 방식으로 구성할 수 있는 유일한 rest-swagger 구성 요소인 경우( application.properties사용)입니다.

이제 애플리케이션에서 ProducerTemplate 을 사용하여 PetStore REST 메서드를 호출할 수 있습니다.

@Autowired
ProducerTemplate template;

String getPetJsonById(int petId) {
    return template.requestBodyAndHeaders("petstore:getPetById", null, "petId", petId);
}