【问题标题】:Camel - how to add request parameter(throwExceptionOnFailure) to url?Camel - 如何将请求参数(throwExceptionOnFailure)添加到 url?
【发布时间】:2017-11-22 08:05:58
【问题描述】:

我有以下路线:

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 中是否有通用方式向 URL 添加请求参数?

类似:

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

在 Camel api 中

【问题讨论】:

    标签: java apache-camel dsl spring-camel


    【解决方案1】:

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

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

    【讨论】:

    • 对我来说很清楚。我想要像 addParamater("throwExceptionOnFailure", false) 这样的方法,我认为它可以在骆驼 api 的某个地方。当然我可以自己写,但如果存在,我想使用内置
    • 根据文档camel.apache.org/servlet,您可以将其设置为标题,键为Exchange.HTTP_QUERY
    • 在您的链接中找不到 Exchange.HTTP_QUERY
    • 至少 .setHeader(Exchange.HTTP_QUERY, constant("throwExceptionOnFailure=false")) 不起作用
    【解决方案2】:

    我认为您必须添加自己的逻辑才能在运行时将端点组合到http 组件。这是因为CamelContext 将在路由本身期间处理它。参数throwExceptionOnFailure 是来自http 组件的属性。

    我不认为通过.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() 的结果编写逻辑来执行您想要的操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-26
      • 1970-01-01
      • 2015-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多