API Gateway용 데이터 모델 및 매핑 템플릿 예시 - Amazon API Gateway

API Gateway용 데이터 모델 및 매핑 템플릿 예시

다음 단원에서는 API Gateway에서 자체 API의 시작점으로 사용할 수 있는 모델 및 매핑 템플릿의 예제를 제공합니다. 데이터 변환에 대한 자세한 내용은 REST API용 매핑 템플릿 섹션을 참조하세요. 데이터 모델에 대한 자세한 내용은 REST API에 대한 데이터 모델 섹션을 참조합니다.

사진 앨범 예시

다음 예시는 API Gateway의 사진 앨범 API를 보여줍니다. 예시 데이터 변환, 추가 모델 및 매핑 템플릿을 제공합니다.

예시 데이터 변환

다음 예시에서는 Velocity Template Language(VTL) 매핑 템플릿을 사용하여 사진에 대한 입력 데이터를 변환하는 방법을 보여줍니다. Velocity Template Language에 대한 자세한 내용은 Apache Velocity - VTL 참조를 참조하세요.

입력 데이터
{ "photos": { "page": 1, "pages": "1234", "perpage": 100, "total": "123398", "photo": [ { "id": "12345678901", "owner": "23456789@A12", "photographer_first_name" : "Saanvi", "photographer_last_name" : "Sarkar", "secret": "abc123d456", "server": "1234", "farm": 1, "title": "Sample photo 1", "ispublic": true, "isfriend": false, "isfamily": false }, { "id": "23456789012", "owner": "34567890@B23", "photographer_first_name" : "Richard", "photographer_last_name" : "Roe", "secret": "bcd234e567", "server": "2345", "farm": 2, "title": "Sample photo 2", "ispublic": true, "isfriend": false, "isfamily": false } ] } }
출력 매핑 템플릿
#set($inputRoot = $input.path('$')) { "photos": [ #foreach($elem in $inputRoot.photos.photo) { "id": "$elem.id", "photographedBy": "$elem.photographer_first_name $elem.photographer_last_name", "title": "$elem.title", "ispublic": $elem.ispublic, "isfriend": $elem.isfriend, "isfamily": $elem.isfamily }#if($foreach.hasNext),#end #end ] }
출력 데이터
{ "photos": [ { "id": "12345678901", "photographedBy": "Saanvi Sarkar", "title": "Sample photo 1", "ispublic": true, "isfriend": false, "isfamily": false }, { "id": "23456789012", "photographedBy": "Richard Roe", "title": "Sample photo 2", "ispublic": true, "isfriend": false, "isfamily": false } ] }

사진 데이터용 입력 모델

입력 데이터에 대한 모델을 정의할 수 있습니다. 이 입력 모델에는 한 장의 사진을 업로드해야 하며 각 페이지당 사진 수가 최소 10장으로 지정되어 있습니다. 이 입력 모델을 사용하여 SDK를 생성하거나 API에 대한 요청 확인을 활성화할 수 있습니다.

{ "$schema": "http://json-schema.org/draft-04/schema#", "title": "PhotosInputModel", "type": "object", "properties": { "photos": { "type": "object", "required" : [ "photo" ], "properties": { "page": { "type": "integer" }, "pages": { "type": "string" }, "perpage": { "type": "integer", "minimum" : 10 }, "total": { "type": "string" }, "photo": { "type": "array", "items": { "type": "object", "properties": { "id": { "type": "string" }, "owner": { "type": "string" }, "photographer_first_name" : {"type" : "string"}, "photographer_last_name" : {"type" : "string"}, "secret": { "type": "string" }, "server": { "type": "string" }, "farm": { "type": "integer" }, "title": { "type": "string" }, "ispublic": { "type": "boolean" }, "isfriend": { "type": "boolean" }, "isfamily": { "type": "boolean" } } } } } } } }

사진 데이터용 출력 모델

출력 데이터에 대한 모델을 정의할 수 있습니다. API에 유형이 엄격하게 지정된 SDK를 생성할 때 필수인 메서드 응답 모델에 이 모델을 사용할 수 있습니다. 이를 통해 출력은 Java 또는 Objective-C의 적절한 클래스에 캐스팅됩니다.

{ "$schema": "http://json-schema.org/draft-04/schema#", "title": "PhotosOutputModel", "type": "object", "properties": { "photos": { "type": "array", "items": { "type": "object", "properties": { "id": { "type": "string" }, "photographedBy": { "type": "string" }, "title": { "type": "string" }, "ispublic": { "type": "boolean" }, "isfriend": { "type": "boolean" }, "isfamily": { "type": "boolean" } } } } } }

사진 데이터용 입력 매핑 템플릿

매핑 템플릿을 정의하여 입력 데이터를 수정할 수 있습니다. 추가 함수 통합 또는 통합 응답을 위해 입력 데이터를 수정할 수 있습니다.

#set($inputRoot = $input.path('$')) { "photos": { "page": $inputRoot.photos.page, "pages": "$inputRoot.photos.pages", "perpage": $inputRoot.photos.perpage, "total": "$inputRoot.photos.total", "photo": [ #foreach($elem in $inputRoot.photos.photo) { "id": "$elem.id", "owner": "$elem.owner", "photographer_first_name" : "$elem.photographer_first_name", "photographer_last_name" : "$elem.photographer_last_name", "secret": "$elem.secret", "server": "$elem.server", "farm": $elem.farm, "title": "$elem.title", "ispublic": $elem.ispublic, "isfriend": $elem.isfriend, "isfamily": $elem.isfamily }#if($foreach.hasNext),#end #end ] } }

뉴스 기사 예

다음 예시는 API Gateway의 뉴스 기사 API를 보여줍니다. 예시 데이터 변환, 추가 모델 및 매핑 템플릿을 제공합니다.

예시 데이터 변환

다음 예시에서는 Velocity Template Language(VTL) 매핑 템플릿을 사용하여 뉴스 기사에 대한 입력 데이터를 변환하는 방법을 보여줍니다. Velocity Template Language에 대한 자세한 내용은 Apache Velocity - VTL 참조를 참조하세요.

입력 데이터
{ "count": 1, "items": [ { "last_updated_date": "2015-04-24", "expire_date": "2016-04-25", "author_first_name": "John", "description": "Sample Description", "creation_date": "2015-04-20", "title": "Sample Title", "allow_comment": true, "author": { "last_name": "Doe", "email": "johndoe@example.com", "first_name": "John" }, "body": "Sample Body", "publish_date": "2015-04-25", "version": "1", "author_last_name": "Doe", "parent_id": 2345678901, "article_url": "http://www.example.com/articles/3456789012" } ], "version": 1 }
출력 매핑 템플릿
#set($inputRoot = $input.path('$')) { "count": $inputRoot.count, "items": [ #foreach($elem in $inputRoot.items) { "creation_date": "$elem.creation_date", "title": "$elem.title", "author": "$elem.author.first_name $elem.author.last_name", "body": "$elem.body", "publish_date": "$elem.publish_date", "article_url": "$elem.article_url" }#if($foreach.hasNext),#end #end ], "version": $inputRoot.version }
출력 데이터
{ "count": 1, "items": [ { "creation_date": "2015-04-20", "title": "Sample Title", "author": "John Doe", "body": "Sample Body", "publish_date": "2015-04-25", "article_url": "http://www.example.com/articles/3456789012" } ], "version": 1 }

뉴스 데이터용 입력 모델

입력 데이터에 대한 모델을 정의할 수 있습니다. 이 입력 모델을 사용하려면 뉴스 기사에 URL, 제목 및 본문이 포함되어야 합니다. 이 입력 모델을 사용하여 SDK를 생성하거나 API에 대한 요청 확인을 활성화할 수 있습니다.

{ "$schema": "http://json-schema.org/draft-04/schema#", "title": "NewsArticleInputModel", "type": "object", "properties": { "count": { "type": "integer" }, "items": { "type": "array", "items": { "type": "object", "required": [ "article_url", "title", "body" ], "properties": { "last_updated_date": { "type": "string" }, "expire_date": { "type": "string" }, "author_first_name": { "type": "string" }, "description": { "type": "string" }, "creation_date": { "type": "string" }, "title": { "type": "string" }, "allow_comment": { "type": "boolean" }, "author": { "type": "object", "properties": { "last_name": { "type": "string" }, "email": { "type": "string" }, "first_name": { "type": "string" } } }, "body": { "type": "string" }, "publish_date": { "type": "string" }, "version": { "type": "string" }, "author_last_name": { "type": "string" }, "parent_id": { "type": "integer" }, "article_url": { "type": "string" } } } }, "version": { "type": "integer" } } }

뉴스 데이터용 출력 모델

출력 데이터에 대한 모델을 정의할 수 있습니다. API에 유형이 엄격하게 지정된 SDK를 생성할 때 필수인 메서드 응답 모델에 이 모델을 사용할 수 있습니다. 이를 통해 출력은 Java 또는 Objective-C의 적절한 클래스에 캐스팅됩니다.

{ "$schema": "http://json-schema.org/draft-04/schema#", "title": "PhotosOutputModel", "type": "object", "properties": { "photos": { "type": "array", "items": { "type": "object", "properties": { "id": { "type": "string" }, "photographedBy": { "type": "string" }, "title": { "type": "string" }, "ispublic": { "type": "boolean" }, "isfriend": { "type": "boolean" }, "isfamily": { "type": "boolean" } } } } } }

뉴스 데이터용 입력 매핑 템플릿

매핑 템플릿을 정의하여 입력 데이터를 수정할 수 있습니다. 추가 함수 통합 또는 통합 응답을 위해 입력 데이터를 수정할 수 있습니다.

#set($inputRoot = $input.path('$')) { "count": $inputRoot.count, "items": [ #foreach($elem in $inputRoot.items) { "last_updated_date": "$elem.last_updated_date", "expire_date": "$elem.expire_date", "author_first_name": "$elem.author_first_name", "description": "$elem.description", "creation_date": "$elem.creation_date", "title": "$elem.title", "allow_comment": "$elem.allow_comment", "author": { "last_name": "$elem.author.last_name", "email": "$elem.author.email", "first_name": "$elem.author.first_name" }, "body": "$elem.body", "publish_date": "$elem.publish_date", "version": "$elem.version", "author_last_name": "$elem.author_last_name", "parent_id": $elem.parent_id, "article_url": "$elem.article_url" }#if($foreach.hasNext),#end #end ], "version": $inputRoot.version }