天天看点

Apache Came组件rest的使用

The rest component allows to define REST endpoints using the Rest DSL and plugin to other Camel components as the REST transport.

代码如下:

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

	<restConfiguration bindingMode="auto" component="restlet" port="3387" />

	<!-- 用法一  -->
	<rest path="/api">
		<!-- 访问路径 http://127.0.0.1:3387/api/products -->
		<get uri="/products">
			<to uri="direct:products" />
		</get>

		<!-- 访问路径 http://127.0.0.1:3387/api/product/{id} -->
		<get uri="/product/{id}">
			<to uri="direct:product" />
		</get>

		<!-- 访问路径 http://127.0.0.1:3387/api/product/create(post请求方式) -->
		<post uri="/product/create">
			<to uri="bean:productNew" />
		</post>
	</rest>

	<route>
		<from uri="direct:products" />
		<setBody>
			<constant>this is products list</constant>
		</setBody>
	</route>

	<route>
		<from uri="direct:product" />
		<to uri="log:show1?showAll=true&multiline=true" />
		<setBody>
			<simple>this is product ${in.header.id}</simple>
		</setBody>
	</route>

	<!-- 用法二 -->
	<route>
		<!-- 访问地址为 http://127.0.0.1:3387/topics -->
		<from uri="rest:get:topics" />
		<setBody>
			<constant>this is topic list</constant>
		</setBody>
	</route>

	<!-- 访问地址为 http://127.0.0.1:3387/topic/111 -->
	<route>
		<from uri="rest:get:topic/{id}" />
		<setBody>
			<simple>this is topic[id=${in.header.id}]</simple>
		</setBody>
	</route>

</camelContext>
           

最后,加入maven依赖

<dependency>
	<groupId>org.apache.camel</groupId>
	<artifactId>camel-restlet</artifactId>
	<version>2.15.3</version>
</dependency>