java骆驼如何将请求参数(ThroweExceptionOnFailure)添加到url?
我有以下路线:
from("quartz2:findAll//myGroup/myTimerName?cron=" + pushProperties.getQuartz())
//.setBody().constant("{ \"id\": \"FBJDBFJHSDBFJSBDfi\" }")
.to("mongodb:mongoBean?database=" + mongoDataConfiguration.getDatabase()
+ "&operation=findAll&collection=" + mongoDataConfiguration.getDataPointCollection())
.process(exchange -> {
exchange.getIn().setBody(objectMapper.writeValueAsString(exchange.getIn().getBody()));
}).streamCaching()
.setHeader(Exchange.HTTP_METHOD, constant(pushProperties.getHttpMethod()))
.setHeader(Exchange.CONTENT_TYPE, constant(MediaType.APPLICATION_JSON_VALUE))
.to(pushProperties.getUrl() + "&throwExceptionOnFailure=false").streamCaching()
如您所见,我使用throwExceptionOnFailure=false
我从配置中获取我的url。但我们发现如果
pushProperties.getUrl()
=localhost:8080/url?action=myaction
如果发生以下情况,则不起作用
pushProperties.getUrl()
=localhost:8080/url
在camel中是否有向URL添加请求参数的通用方法
比如:
private String buildUrl() {
String url = pushProperties.getUrl();
return url + (url.contains("?") ? "&" : "?") + "throwExceptionOnFailure=false";
}
骆驼内部api
# 1 楼答案
这是因为在
localhost:8080/url
的情况下,在附加它之后会变成这样localhost:8080/url&throwExceptionOnFailure=false
哪个是错误的
应该是
localhost:8080/url?throwExceptionOnFailure=false
,在第一种情况下,它可以工作,您已经有一个requestpatam(
?action=myaction
),因此下一个可以用符号(&;)添加# 2 楼答案
我认为您必须添加自己的逻辑,以便在运行时将端点组合到
http
组件中。这是因为CamelContext
将在路由本身期间处理它。参数throwExceptionOnFailure
是来自http
组件的属性我认为通过
.setHeader(Exchange.HTTP_QUERY, constant("throwExceptionOnFailure=false"))
添加参数不应该起作用,因为这些参数将在http
组件得到处理后进行评估,例如进入URL目标。请看一下"How to use a dynamic URI in to()":您可以使用simple expression编写一个逻辑,根据
pushProperties.getUrl()
的结果执行所需操作