php做网站项目的流程,商业推广,定制网站开发方案,手机小说网站建设在Camel中公开HTTP终结点的方法有很多#xff1a;jetty#xff0c;tomcat#xff0c;servlet#xff0c;cxfrs和restlet。 其中的两个组件– cxfrs和restlet也只需几行代码即可支持REST语义。 这个简单的示例演示了如何使用camel-restlet和camel-jdbc进行CRUD操作。 四个HT… 在Camel中公开HTTP终结点的方法有很多jettytomcatservletcxfrs和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创建这样的应用程序很简单。 添加所有必要的依赖项restletspringjdbc…后配置web.xml来加载Camel上下文 context-paramparam-namecontextConfigLocation/param-nameparam-valueclasspath:camel-config.xml/param-value/context-paramlistenerlistener-classorg.springframework.web.context.ContextLoaderListener/listener-class/listener 并映射Restlet servlet servletservlet-nameRestletServlet/servlet-nameservlet-classorg.restlet.ext.spring.SpringServerServlet/servlet-classinit-paramparam-nameorg.restlet.component/param-nameparam-valueRestletComponent/param-value/init-param
/servlet
servlet-mappingservlet-nameRestletServlet/servlet-nameurl-pattern/rs/*/url-pattern
/servlet-mapping 在Spring上下文中还有更多的Restlet和一个内存中的数据源设置代码 bean idRestletComponent classorg.restlet.Component/bean idRestletComponentService classorg.apache.camel.component.restlet.RestletComponentconstructor-arg index0ref beanRestletComponent//constructor-arg/beanjdbc:embedded-database iddataSource typeHSQLjdbc:script locationclasspath:sql/init.sql//jdbc:embedded-database 完成所有设置后下一步是创建将处理HTTP请求并执行适当的CRUD操作的骆驼路由。 第一个是createUser路由该路由仅使用POST请求中的参数执行SQL插入命令并在响应正文中返回新创建的用户 route idcreateUserfrom urirestlet:/user?restletMethodPOST/setBodysimpleinsert into user(firstName, lastName) values(${header.firstName},${header.lastName}); /simple/setBodyto urijdbc:dataSource/setBodysimpleselect * from user ORDER BY id desc LIMIT 1/simple/setBodyto urijdbc:dataSource/
/route “ manipulateUser”路由处理GETPUT和DELETE HTTP方法但是根据使用的方法它执行不同的SQL命令 route idmanipulateUserfrom urirestlet:/user/{userId}?restletMethodsGET,PUT,DELETE/choicewhensimple${header.CamelHttpMethod} GET/simplesetBodysimpleselect * from user where id ${header.userId}/simple/setBody/whenwhensimple${header.CamelHttpMethod} PUT/simplesetBodysimpleupdate user set firstName${header.firstName}, lastName${header.lastName} where id ${header.userId}/simple/setBody/whenwhensimple${header.CamelHttpMethod} DELETE/simplesetBodysimpledelete from user where id ${header.userId}/simple/setBody/whenotherwisestop//otherwise/choiceto urijdbc:dataSource/
/route 列出所有用户的最后一条路线是不言而喻的 route idlistUsersfrom urirestlet:/users/setBodyconstantselect * from user/constant/setBodyto urijdbc:dataSource/
/route 如果您想查看应用程序的运行情况请从github获取源代码并通过键入以下命令使用嵌入式maven-jetty插件运行它如果已安装curl甚至可以尝试一些快速查询 要创建用户请使用firstName和lastName参数发出http POST请求 curl -d firstNametestlastNameuser http://localhost:8080/rs/user/ 要更新现有用户请使用firstName和lastName参数发出http PUT请求 curl -X PUT -d firstNameupdatedlastNameuser 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