繁体   English   中英

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

[英]Camel - how to add request parameter(throwExceptionOnFailure) to 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

在骆驼中是否有通用的方式将请求参数添加到 URL?

就像是:

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

骆驼内部 api

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

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

我认为您必须在运行时添加自己的逻辑以将终结点组合到http组件。 这是因为CamelContext将在路由本身期间CamelContext进行处理。 参数throwExceptionOnFailurehttp组件的属性。

我不认为应该通过.setHeader(Exchange.HTTP_QUERY, constant("throwExceptionOnFailure=false"))添加参数.setHeader(Exchange.HTTP_QUERY, constant("throwExceptionOnFailure=false"))因为这些参数将在处理http组件(例如进入URL目标)后进行评估。 请看看“如何在to()中使用动态URI”

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

您可以使用简单表达式根据pushProperties.getUrl()的结果编写逻辑以执行所需的pushProperties.getUrl()

我不喜欢 Camel 在这种情况下如何配置 HTTP 组件,但事实就是如此。

我的建议是创建一个 map 的配置和 append 的参数,然后使用“&”进行手动连接,然后将 append 连接到主要的 url。

我这样做是这样的:

public class MyProcessor {
    /**
     * Part of Camel HTTP component config are done with URL query parameters.
     */
    private static final Map<String, String> COMMON_QUERY_PARAMS = Map.of(
            // do not throw HttpOperationFailedException; we handle them ourselves
            "throwExceptionOnFailure", "false"
    );

    @Handler
    void configure(Exchange exchange, ...) {
        ...
        Map<String, String> queryParams = new HashMap<>();
        queryParams.put("foo", "bar");
        message.setHeader(Exchange.HTTP_QUERY, mergeAndJoin(queryParams));
        ...
    }
    
    private String mergeAndJoin(Map<String, String> queryParams) {
        // make sure HTTP config params put after event params
        return Stream.concat(queryParams.entrySet().stream(), COMMON_QUERY_PARAMS.entrySet().stream())
                .map(entry -> entry.getKey() + "=" + entry.getValue())
                .collect(Collectors.joining("&"));
    }

}

请注意,toD 需要优化,但在这种情况下,不能使用 HTTP_QUERY。

使用优化组件时,您不能使用标头 Exchange.HTTP_PATH 和 Exchange.HTTP_QUERY 提供动态值来覆盖 toD 中的 uri。 如果您想使用这些标头,请改用普通的 DSL。 换句话说,这些标头由 toD 在内部使用以承载端点的动态详细信息。

https://camel.apache.org/components/3.20.x/eips/toD-eip.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM