Java 如何在 Camel HTTP 代理中获取和设置参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23307707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get and set parameters in Camel HTTP Proxy
提问by bks4line
lets say for an example i have a code:
举个例子,我有一个代码:
from(servlet://abc?id={id}&name={name}).process(new Processor(){
@Override
public void process(Exchange arg0) throws Exception {
id = arg0.getIn().getHeader("id", String.class);
id_type = arg0.getIn().getHeader("name",String.class);
System.out.println(id);
System.out.println(name);
String url = "//example.com/"+id+"?name="+name;
System.out.println(url);
/*Thread.sleep(10000);*/
}.setHeader(Exchange.HTTP).to("http:"+url+"&bridgeEndpoint=true&throwExceptionOnFailure=false)"
I dont see my url there. its showing null value. how to solve this problem? I used to set this string in Exchange header but its giving me java.lang.IllegalArgumentException:
我在那里看不到我的网址。它显示空值。如何解决这个问题呢?我曾经在 Exchange 标头中设置此字符串,但它给了我 java.lang.IllegalArgumentException:
采纳答案by Peter Keller
Try following route:
尝试以下路线:
from("servlet://abc")
.process(new Processor(){
@Override
public void process(Exchange exchange) throws Exception {
// Camel will populate all request.parameter and request.headers,
// no need for placeholders in the "from" endpoint
String id = exchange.getIn().getHeader("id", String.class);
String name = exchange.getIn().getHeader("name", String.class);
// This URI will override http://dummyhost
exchange.getIn().setHeader(Exchange.HTTP_URI, "http://example.com");
// Add input path. This will override the original input path.
// If you need to keep the original input path, then add the id to the
// URI above instead
exchange.getIn().setHeader(Exchange.HTTP_PATH, id);
// Add query parameter such as "?name=xxx"
exchange.getIn().setHeader(Exchange.HTTP_QUERY, "name="+name);
}
.to("http://dummyhost")
If you request is http://localhost:8080/hello/world?id=111&name=moon
, then it should be forwarded to http://example.com/111?name=moon
.
如果您的请求是http://localhost:8080/hello/world?id=111&name=moon
,则应将其转发给http://example.com/111?name=moon
。
回答by Willem Jiang
The url cannot be know when camel setup the route.
骆驼设置路线时无法知道网址。
You can use Exchange.HTTP_URI message header to override the setting of http endpoint.
您可以使用 Exchange.HTTP_URI 消息头来覆盖 http 端点的设置。