在Camel中公開HTTP終結點的方法有很多:jetty,tomcat,servlet,cxfrs和restlet。 其中的兩個組件– cxfrs和restlet也只需幾行代碼即可支持REST語義。 這個簡單的示例演示了如何使用camel-restlet和camel-jdbc進行CRUD操作。 四個HTTP動詞執行不同的操作,并映射到以下單個URI模板:
- POST –創建一個新用戶: / user
- GET –請求URI指定的用戶的當前狀態: / user / {userId}
- PUT –使用新信息更新給定URI上的用戶 : / user / {userId}
- 刪除–刪除由給定URI標識的用戶: / user / {userId}
還有一個/ users URI,它返回所有用戶,無論使用哪種HTTP方法。 用Camel創建這樣的應用程序很簡單。 添加所有必要的依賴項(restlet,spring,jdbc…)后,配置web.xml來加載Camel上下文:
<context-param><param-name>contextConfigLocation</param-name><param-value>classpath:camel-config.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
并映射Restlet servlet
<servlet><servlet-name>RestletServlet</servlet-name><servlet-class>org.restlet.ext.spring.SpringServerServlet</servlet-class><init-param><param-name>org.restlet.component</param-name><param-value>RestletComponent</param-value></init-param>
</servlet>
<servlet-mapping><servlet-name>RestletServlet</servlet-name><url-pattern>/rs/*</url-pattern>
</servlet-mapping>
在Spring上下文中,還有更多的Restlet和一個內存中的數據源設置代碼:
<bean id="RestletComponent" class="org.restlet.Component"/><bean id="RestletComponentService" class="org.apache.camel.component.restlet.RestletComponent"><constructor-arg index="0"><ref bean="RestletComponent"/></constructor-arg></bean><jdbc:embedded-database id="dataSource" type="HSQL"><jdbc:script location="classpath:sql/init.sql"/></jdbc:embedded-database>
完成所有設置后,下一步是創建將處理HTTP請求并執行適當的CRUD操作的駱駝路由。 第一個是createUser路由,該路由僅使用POST請求中的參數執行SQL插入命令,并在響應正文中返回新創建的用戶:
<route id="createUser"><from uri="restlet:/user?restletMethod=POST"/><setBody><simple>insert into user(firstName, lastName) values('${header.firstName}','${header.lastName}'); </simple></setBody><to uri="jdbc:dataSource"/><setBody><simple>select * from user ORDER BY id desc LIMIT 1</simple></setBody><to uri="jdbc:dataSource"/>
</route>
“ manipulateUser”路由處理GET,PUT和DELETE HTTP方法,但是根據使用的方法,它執行不同的SQL命令:
<route id="manipulateUser"><from uri="restlet:/user/{userId}?restletMethods=GET,PUT,DELETE"/><choice><when><simple>${header.CamelHttpMethod} == 'GET'</simple><setBody><simple>select * from user where id = ${header.userId}</simple></setBody></when><when><simple>${header.CamelHttpMethod} == 'PUT'</simple><setBody><simple>update user set firstName='${header.firstName}', lastName='${header.lastName}' where id = ${header.userId}</simple></setBody></when><when><simple>${header.CamelHttpMethod} == 'DELETE'</simple><setBody><simple>delete from user where id = ${header.userId}</simple></setBody></when><otherwise><stop/></otherwise></choice><to uri="jdbc:dataSource"/>
</route>
列出所有用戶的最后一條路線是不言而喻的:
<route id="listUsers"><from uri="restlet:/users"/><setBody><constant>select * from user</constant></setBody><to uri="jdbc:dataSource"/>
</route>
如果您想查看應用程序的運行情況,請從github獲取源代碼,并通過鍵入以下命令使用嵌入式maven-jetty插件運行它:如果已安裝curl,甚至可以嘗試一些快速查詢:
要創建用戶,請使用firstName和lastName參數發出http POST請求
curl -d 'firstName=test&lastName=user' http://localhost:8080/rs/user/
要更新現有用戶,請使用firstName和lastName參數發出http PUT請求
curl -X PUT -d 'firstName=updated&lastName=user' http://localhost:8080/rs/user/2
要檢索現有用戶,請發出帶有userId作為URL一部分的http GET請求
curl -X GET http://localhost:8080/rs/user/2
要刪除現有用戶,請發出http DELETE請求,并將userId作為URL的一部分
curl -X DELETE http://localhost:8080/rs/user/2
要檢索所有現有用戶,請向用戶url發出http GET請求
curl -X GET http://localhost:8080/rs/users
參考:來自OFBIZian博客的JCG合作伙伴 Bilgin Ibryam提供的REST with Apache Camel 。
翻譯自: https://www.javacodegeeks.com/2013/03/rest-with-apache-camel.html