Camel - 如何将请求参数(throwExceptionOnFailure)添加到url?

时间:2017-11-22 08:05:58

标签: java apache-camel dsl spring-camel

我有以下路线:

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

我从配置中获取了我的网址。但是我们发现它有效,如果

pushProperties.getUrl() = localhost:8080/url?action=myaction

并且在

的情况下不起作用

pushProperties.getUrl() = localhost:8080/url

在camel中是否有universla方式向URL添加请求参数?

类似的东西:

private String buildUrl() {
    String url = pushProperties.getUrl();
    return url + (url.contains("?") ? "&" : "?") + "throwExceptionOnFailure=false";
}

在Camel api中

2 个答案:

答案 0 :(得分:0)

这是因为在localhost:8080/url的情况下,在追加之后就变成了这样的

localhost:8080/url&throwExceptionOnFailure=false
这是错误的 它应该是
localhost:8080/url?throwExceptionOnFailure=false
在第一种情况下,它已经有了一个requestpatam(?action=myaction),所以下一个可以添加&符号(&)

答案 1 :(得分:0)

我认为您必须添加自己的逻辑,以便在运行时将端点组成http组件。这是因为CamelContext将在路线本身期间处理它。参数throwExceptionOnFailurehttp组件的属性。

我认为通过.setHeader(Exchange.HTTP_QUERY, constant("throwExceptionOnFailure=false"))添加参数无效,因为这些参数将在http组件处理后进行评估,例如:进入URL目的地。请看一下"How to use a dynamic URI in to()"

.toD(pushProperties.getUrl() + "&throwExceptionOnFailure=false")

您可以使用simple expression根据pushProperties.getUrl()的结果编写逻辑来执行您想要的操作。

相关问题