在 Camel SpringBoot 项目中传播请求参数值
Propagate request param value in Camel SpringBoot project
你好,我有 Spring Boot 2 项目,我正在使用 camel 作为路由。
我有一个骆驼休息端点和一条骆驼路线:
rest("/").produces("application/json")
.get("hello")
.param().name("url").type(RestParamType.query)
.dataType("String").endParam()
.to("direct:hello");
/////////////////////////////////////////////
System.out.println("starterd");
boolean startupRoute = true;
from("direct:hello").autoStartup(startupRoute)
.tracing()
.streamCaching()
.process(exchange -> exchange.getIn()
.setBody(exchange.getIn()
.getBody()))
.convertBodyTo(String.class)
.marshal()
.json(JsonLibrary.Jackson)
.setHeader("Content-Type", constant("application/json"))
.setHeader("Accept", constant("application/json"))
.setHeader(Exchange.HTTP_METHOD, constant("GET"))
.setHeader(Exchange.HTTP_URI).header("url")
.log(LoggingLevel.INFO, "${body}")
.removeHeader(Exchange.HTTP_PATH)
.to("http4://url")
.log(LoggingLevel.INFO, "This is my body: ${body}")
.to("activemq://hello?exchangePattern=InOnly");
System.out.println("finished");
我想做的是当我发送这样的请求时:
http://localhost:8080/camel/hello/?url=http://localhost:8081/hi
路由中第一个.to()要设置的url值:
.to("{url}?bridgeEndpoint=true")
我也尝试过 spring boot rest controller 但我仍然无法获取 .to(${url}
中的参数值
@GetMapping(value = "/finally")
public String sendFromEndpointToActiveMq(@RequestParam(value = "url") String url) throws Exception {
producerTemplate.sendBody("direct:hello", url);
return "done";
编辑:我已经编辑了路线
您必须使用 http 组件 http://camel.apache.org/http4.html
You can override the HTTP endpoint URI by adding a header with the key, Exchange.HTTP_URI, on the message
.setHeader(Exchange.HTTP_URI).header("url")
.to("http4://dummy")
试试这个
System.out.println("starterd");
boolean startupRoute = true;
from("direct:hello").autoStartup(startupRoute)
.tracing()
.streamCaching()
.process(exchange -> exchange.getIn()
.setBody(exchange.getIn()
.getBody()))
.convertBodyTo(String.class)
.marshal()
.json(JsonLibrary.Jackson)
.setHeader("Content-Type", constant("application/json"))
.setHeader("Accept", constant("application/json"))
.setHeader(Exchange.HTTP_METHOD, constant("GET"))
.setHeader(Exchange.HTTP_URI)
.header("url")
.log(LoggingLevel.INFO, "${body}")
.removeHeader(Exchange.HTTP_PATH)
.to("http4://url")
.to("direct:hi");
from("direct:hi").log(LoggingLevel.INFO, "This is my body: ${body}")
.recipientList(simple("activemq://${header.activemq}"+"?exchangePattern=InOnly"));
System.out.println("finished");
你好,我有 Spring Boot 2 项目,我正在使用 camel 作为路由。
我有一个骆驼休息端点和一条骆驼路线:
rest("/").produces("application/json")
.get("hello")
.param().name("url").type(RestParamType.query)
.dataType("String").endParam()
.to("direct:hello");
/////////////////////////////////////////////
System.out.println("starterd");
boolean startupRoute = true;
from("direct:hello").autoStartup(startupRoute)
.tracing()
.streamCaching()
.process(exchange -> exchange.getIn()
.setBody(exchange.getIn()
.getBody()))
.convertBodyTo(String.class)
.marshal()
.json(JsonLibrary.Jackson)
.setHeader("Content-Type", constant("application/json"))
.setHeader("Accept", constant("application/json"))
.setHeader(Exchange.HTTP_METHOD, constant("GET"))
.setHeader(Exchange.HTTP_URI).header("url")
.log(LoggingLevel.INFO, "${body}")
.removeHeader(Exchange.HTTP_PATH)
.to("http4://url")
.log(LoggingLevel.INFO, "This is my body: ${body}")
.to("activemq://hello?exchangePattern=InOnly");
System.out.println("finished");
我想做的是当我发送这样的请求时:
http://localhost:8080/camel/hello/?url=http://localhost:8081/hi
路由中第一个.to()要设置的url值:
.to("{url}?bridgeEndpoint=true")
我也尝试过 spring boot rest controller 但我仍然无法获取 .to(${url}
中的参数值@GetMapping(value = "/finally")
public String sendFromEndpointToActiveMq(@RequestParam(value = "url") String url) throws Exception {
producerTemplate.sendBody("direct:hello", url);
return "done";
编辑:我已经编辑了路线
您必须使用 http 组件 http://camel.apache.org/http4.html
You can override the HTTP endpoint URI by adding a header with the key, Exchange.HTTP_URI, on the message
.setHeader(Exchange.HTTP_URI).header("url")
.to("http4://dummy")
试试这个
System.out.println("starterd");
boolean startupRoute = true;
from("direct:hello").autoStartup(startupRoute)
.tracing()
.streamCaching()
.process(exchange -> exchange.getIn()
.setBody(exchange.getIn()
.getBody()))
.convertBodyTo(String.class)
.marshal()
.json(JsonLibrary.Jackson)
.setHeader("Content-Type", constant("application/json"))
.setHeader("Accept", constant("application/json"))
.setHeader(Exchange.HTTP_METHOD, constant("GET"))
.setHeader(Exchange.HTTP_URI)
.header("url")
.log(LoggingLevel.INFO, "${body}")
.removeHeader(Exchange.HTTP_PATH)
.to("http4://url")
.to("direct:hi");
from("direct:hi").log(LoggingLevel.INFO, "This is my body: ${body}")
.recipientList(simple("activemq://${header.activemq}"+"?exchangePattern=InOnly"));
System.out.println("finished");