正如官方所说:
ApacheCamel是一个开源集成框架,它使您能够快速轻松地集成使用或生成数据的各种系统
此次博主来介绍下camel http组件的使用.本博文将使用 camel xml dsl
我们来完成用camel调用http接口并将接口返回值保存到数据库中
如果你使用springboot集成camel,你可能需要添加以下依赖:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <dependency> <groupId>org.apache.camel.springboot</groupId> <artifactId>camel-jackson-starter</artifactId> </dependency> <dependency> <groupId>org.apache.camel.springboot</groupId> <artifactId>camel-sql-starter</artifactId> </dependency> <dependency> <groupId>org.apache.camel.springboot</groupId> <artifactId>camel-jdbc-starter</artifactId> </dependency> <dependency> <groupId>org.apache.camel.springboot</groupId> <artifactId>camel-http-starter</artifactId> </dependency>
|
创建一个demo表
1 2 3 4 5 6 7
| CREATE TABLE `camel_http_component_demo` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键自动增加', `key1` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'key1', `key2` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'key2', `key3` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'key3', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
|
这看起来如下:
id |
key1 |
key2 |
key3 |
1 |
测试数据1 |
测试数据1-2 |
测试数据1-3 |
GET请求
我们来编写一个controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| @GetMapping("/testCamelGet") @ResponseBody public Map<String, Object> testCamelGet( String paramName, Integer paramAge, @RequestHeader("headerKey") String headerKey ) { System.out.println("paramName: " + paramName); System.out.println("paramAge: " + paramAge); System.out.println("headerKey: " + headerKey);
Map<String, Object> resultMap = new HashMap<>(); resultMap.put("code", "1"); resultMap.put("message", "请求完成!"); List<Map<String, Object>> data = new ArrayList<Map<String, Object>>() {{ add( new HashMap<String, Object>() {{ put("resultName", "No.1 这是服务器返回的哦: " + paramName); put("resultAge", "No.1 这是服务器返回的哦: " + paramAge); put("resultHeaderKey", "No.1 这是服务器返回的哦: " + headerKey); }} ); add( new HashMap<String, Object>() {{ put("resultName", "No.2 这是服务器返回的哦: " + paramName); put("resultAge", "No.2 这是服务器返回的哦: " + paramAge); put("resultHeaderKey", "No.2 这是服务器返回的哦: " + headerKey); }} ); add( new HashMap<String, Object>() {{ put("resultName", "No.1 这是服务器返回的哦: " + paramName); put("resultAge", "No.1 这是服务器返回的哦: " + paramAge); put("resultHeaderKey", "No.1 这是服务器返回的哦: " + headerKey); }} ); }}; resultMap.put("data", data); return resultMap; }
|
这看起来如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| { "code": "1", "data": [ { "resultHeaderKey": "No.1 这是服务器返回的哦: asdfadsfadsfasdf", "resultAge": "No.1 这是服务器返回的哦: 15", "resultName": "No.1 这是服务器返回的哦: 张三" }, { "resultHeaderKey": "No.2 这是服务器返回的哦: asdfadsfadsfasdf", "resultAge": "No.2 这是服务器返回的哦: 15", "resultName": "No.2 这是服务器返回的哦: 张三" }, { "resultHeaderKey": "No.1 这是服务器返回的哦: asdfadsfadsfasdf", "resultAge": "No.1 这是服务器返回的哦: 15", "resultName": "No.1 这是服务器返回的哦: 张三" } ], "message": "请求完成!" }
|
我们需要将接口返回的 data 节点下面的的数据保存到表中
camel xml dsl:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| <routes xmlns="http://camel.apache.org/schema/spring">
<route xmlns="http://camel.apache.org/schema/spring" id="route1"> <from uri="direct:startHttp"/> <setHeader name="CamelHttpMethod"> <constant>GET</constant> </setHeader> <setHeader name="headerKey"> <constant>模拟key--sdfafsadjjfasdjfasdfsadfsdafsdafsdf</constant> </setHeader> <to uri="http://127.0.0.1:8081/testCamelGet?paramName=张三&paramAge=10"/> <to uri="direct:handlerHttpResponse"/> </route>
<route xmlns="http://camel.apache.org/schema/spring" id="route2"> <from uri="direct:handlerHttpResponse"/>
<unmarshal id="unmarshal1"> <json library="Jackson"/> </unmarshal>
<to uri="log:result"/>
<split> <simple>${body[data]}</simple> <to uri="sql:insert into camel_http_component_demo(key1,key2,key3) values (:#resultHeaderKey,:#resultAge,:#resultName)"/> </split>
</route> </routes>
|
创建xml文件http_get.xml,将其放到maven resources目录下,且运行:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| package net.wchar.camel.sample;
import org.apache.camel.CamelContext; import org.apache.camel.ExtendedCamelContext; import org.apache.camel.ProducerTemplate; import org.apache.camel.model.ModelCamelContext; import org.apache.camel.model.RoutesDefinition; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean;
import java.io.InputStream;
@SpringBootApplication public class CamelSampleApplication { @Autowired ProducerTemplate producerTemplate; @Autowired CamelContext camelContext; public static void main(String[] args) { SpringApplication.run(CamelSampleApplication.class, args); } @Bean CommandLineRunner runner() { return args -> { InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream("http_get.xml"); ExtendedCamelContext ecc = camelContext.adapt(ExtendedCamelContext.class); RoutesDefinition routeDefinition = (RoutesDefinition) ecc .getXMLRoutesDefinitionLoader() .loadRoutesDefinition(ecc, resourceAsStream); ModelCamelContext adapt = camelContext.adapt(ModelCamelContext.class); adapt.addRouteDefinitions(routeDefinition.getRoutes()); System.out.println("load xml success!"); producerTemplate.requestBody("direct:startHttp", null, String.class); }; } }
|
哈哈不出所料成功! 开心o( ̄▽ ̄)ブ
POST请求
这里我们就复制上面的controller改改了o( ̄▽ ̄)ブ
Controller:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| @Data @ToString public static class TestDomain { private String paramName; private String paramAge; }
@PostMapping("/testCamelPost") @ResponseBody public Map<String, Object> testCamelPost( @RequestBody TestDomain testDomain, @RequestHeader("headerKey") String headerKey ) { System.out.println("testDomain: " + testDomain); System.out.println("headerKey: " + headerKey);
Map<String, Object> resultMap = new HashMap<>(); resultMap.put("code", "1"); resultMap.put("message", "请求完成!"); List<Map<String, Object>> data = new ArrayList<Map<String, Object>>() {{ add( new HashMap<String, Object>() {{ put("resultName", "No.1 这是服务器返回的哦: " + testDomain.getParamName()); put("resultAge", "No.1 这是服务器返回的哦: " + testDomain.getParamAge()); put("resultHeaderKey", "No.1 这是服务器返回的哦: " + headerKey); }} ); add( new HashMap<String, Object>() {{ put("resultName", "No.2 这是服务器返回的哦: " + testDomain.getParamName()); put("resultAge", "No.2 这是服务器返回的哦: " + testDomain.getParamAge()); put("resultHeaderKey", "No.2 这是服务器返回的哦: " + headerKey); }} ); add( new HashMap<String, Object>() {{ put("resultName", "No.1 这是服务器返回的哦: " + testDomain.getParamName()); put("resultAge", "No.1 这是服务器返回的哦: " + testDomain.getParamAge()); put("resultHeaderKey", "No.1 这是服务器返回的哦: " + headerKey); }} ); }}; resultMap.put("data", data); return resultMap; }
|
camel xml dsl:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| <routes xmlns="http://camel.apache.org/schema/spring"> <route xmlns="http://camel.apache.org/schema/spring" id="route1"> <from uri="direct:startHttp"/> <setHeader name="CamelHttpMethod"> <constant>POST</constant> </setHeader> <setHeader name="Content-Type"> <constant>application/json; charset=utf-8</constant> </setHeader> <setHeader name="headerKey"> <constant>我是headerKey</constant> </setHeader> <setBody> <constant>{"paramName": "李四", "paramAge": "100"}</constant> </setBody> <to uri="http://127.0.0.1:8081/testCamelPost"/> <to uri="direct:handlerHttpResponse"/> </route>
<route xmlns="http://camel.apache.org/schema/spring" id="route2"> <from uri="direct:handlerHttpResponse"/>
<unmarshal id="unmarshal1"> <json library="Jackson"/> </unmarshal>
<to uri="log:result"/>
<split> <simple>${body[data]}</simple> <to uri="sql:insert into camel_http_component_demo(key1,key2,key3) values (:#resultHeaderKey,:#resultAge,:#resultName)"/> </split> </route> </routes>
|
再次运行,成功!
这里可能有眼睑的同学发现了,怎么有乱码啊,这是因为http头部是不能传中文的哦.
好了,本次到此就结束了.欢迎光临 宽字符(wchar.net).
o( ̄▽ ̄)ブ