279장. 예: PetStore

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

예를 들어, PetStore 에서 REST API를 사용하고자 하는 경우, 표준 URI와 원하는 작업 ID를 OpenApi 사양에서 참조하거나 사양을 다운로드하여 CLASSPATH의 루트(루트)로 저장하여 자동으로 사용됩니다. Undertow 구성 요소를 사용하여 Spring Boot에 대한 모든 요청 및 Camels excelent 지원을 수행합니다.

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

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

@Autowired
ProducerTemplate template;

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