日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

REST与Apache Camel

發布時間:2023/12/3 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 REST与Apache Camel 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在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

總結

以上是生活随笔為你收集整理的REST与Apache Camel的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。