REST Component

1. REST란?

REST DSL 이용하여 정의한 REST endpoint를 Camel plug-in하여 REST transport 사용한다.

[URI format]

rest://method:path[:uriTemplate]?[options]

Option Description
Method HTTP method(get/post/put/delete)
Path Base path
uriTemplate URI template
Consumers REST service에서 받아들일 media type (text/xml, application/json …)
produces REST service에서 return할 media type
<rest path="/say"> <!-- path -->
  <get uri="/hello">
      <route>
        <transform>
          <constant>Hello World</constant>
        </transform>
      </route>
  </get>
  <get uri="/bye">  <!-- uriTemplate -->
      <route>
        <transform>
          <constant>Bye World</constant>
        </transform>
      </route>
  </get>
</rest>
  • <rest> 안에는 2개의 <get>, <route>가 존재하는데 2개 모두 같은 path를 사용하지만, 다른 uriTemplate를 사용하고 있다.
  • …/say/hello 주소로 접속하면 “Hello World” 출력이 되고, …/say/bye 주소로 접속하면 “Bye World” 출력된다.
[uriTemplate]

HTTP URI를 정의한다.
uriTemplate는 경로와 쿼리(query), 두 부분으로 구성되어 있다.
ex) /user/Request/{what}?name={header}
/user/Request/{what} : 경로
/user/Request : 리터럴
/{what} : 변수
?name={header} : 쿼리(query)
ⅰ. 경로
‘/’로 구분된 세그먼트로 구성
각 세그먼트는 리터럴(고정 데이터), 변수(중괄호({})로 묶음) 또는 와일드 카드로 구성
ⅱ. 쿼리(query)
생략 가능
‘&’로 구분된 이름/값 쌍을 지정, 짝이 이루어져 있어야만 사용 가능
Query의 오른쪽에만 변수를 포함 가능
주의 : uriTemplate 문자열 내의 모든 변수 이름은 고유성을 유지해야 한다. (대/소문자 구분 X)

2. REST DSL

[REST service 정의하는 방법]

ⅰ. JAX-RS 이용
JAX-RS 이용하여 REST service 정의
@Path, @GET, @POST, @PUT, @DELETE, @PathParam annotation 등으로 구현
ⅱ. Camel REST component
REST service를 정의할 수 있는 camel component 이용

[REST DSL]

End users에게 REST service를 정의하고 REST verb(get, post, put, delete)을 사용하게 해줄 수 있는 DSL
REST DSL을 지원하는 camel component만이 사용할 수 있다.
REST DSL 지원하는 camel component : camel-netty-http, camel-netty4-http, camel-jetty, camel-restlet, camel-servlet, camel-spark-rest …

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 	xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

	<bean id="jetty" class="org.apache.camel.component.jetty9.JettyHttpComponent9">
	</bean>

 	<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">

 		<restConfiguration component="jetty" scheme="http" host="localhost" port="10050">
 		</restConfiguration>

 	 	<rest path="/say"> <!-- base path -->
 	 		<get uri="/hello"> <!-- uriTemplate -->
        <route>
          <transform>
            <constant>Hello!</constant>
          </transform>
        </route>
 	 		</get>
 	 	</rest>			
	</camelContext>
 </beans>
[Option]
<restConfiguration component="jetty" scheme="http" host="localhost" port="10050" bindingMode="auto">
</restConfiguration>

ⅰ. component
REST transport로 사용할 Camel REST component
ex) jetty, restlet, spark-rest, servlet …
ⅱ. scheme
(default) http
REST service에서 사용할 scheme (http/https)
ⅲ. port
REST service에 접근할 포트 번호
ⅳ. host
REST service에서 expose할 hostname
ⅴ. bindingMode
REST service에서의 request, response에 대한 binding mode 설정
Incoming, outgoing message 모두 적용
① off (default)
Incoming, outgoing message에 대해 자동으로 binding X
② auto
binding 활성화 + JSON, XML 중 classpath에 library가 포함되어 있는 format만 지원
③ json
JSON binding만 활성화 (library에 camel-jackson을 포함해야 사용 가능)
④ xml
XML binding만 활성화 (library에 camel-jaxb을 포함해야 사용 가능)
⑤ json_xml
XML, JSON binding 모두 활성화
“auto”와 다른 점

  • auto는 JSON, XML 중 단 하나의 library라도 존재한다면 오류 없이 수행 가능
  • json_xml은 JSON, XML binding에 필요한 camel-jackson와 camel-jaxb, 2개의 library 중 하나라도 존재하지 않는다면 사용할 수 X
[consumes, produces]

<rest>와 REST method(, …) 모두사용 가능 \ : 정의한 REST service 모든 범위에 적용 REST method : 정의한 REST service의 해당 method에만 적용 content-type 지정할 때는 “produces” option 사용

  • 종류 (type)
    • text/plain
    • text/html
    • application/json
    • application/xml
<rest path="/say" consumes="application/json" produces="application/json">
  <get uri="/hello">
    <to uri="bean:userService" />
  </get>
</rest>
  • <rest>에 consumes, produces를 application/json으로 설정
  • “/say”로 시작하는 모든 rest service에 적용하는데, application/json으로 content-type 설정한다.
[type, outType]

ⅰ. type
Incoming(input) type 명시
ⅱ. outType
HTTP response message(outgoing)으로 나타낼 Java type 명시
(주의) bindingMode가 활성화 되어있어야만 type, outType 사용 가능

<rest path="/user">
    <get uri="/{id}" outType="kr.co.bizframe.camel.rest.User">
      <to uri="bean:userService?method=getUser(${header.id})" />
    </get>
    <put uri="/update" type="kr.co.bizframe.camel.rest.User">
      <!-- -->
      <to uri="bean:userService?method=updateUser" />
    </put>
</rest>
  • “…/user/{id}”로 접속할 때는 output type으로 kr.co.bizframe.camel.rest.User로 지정함으로써 service 결과를 지정한 Java type에 맞게 출력해준다.
  • “…/update”로 접속할 때는 incoming type을kr.co.bizframe.camel.rest.User class로 지정했다. 지정한 class format대로 input이 되어 있지 않는다면 오류가 발생한다.