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

问题描述 投票:0回答:3

我有以下路线:

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 内部

java apache-camel dsl spring-camel
3个回答
1
投票

那是因为在

localhost:8080/url
的情况下,追加后就变成这样了

localhost:8080/url&throwExceptionOnFailure=false

这是错误的
应该是
localhost:8080/url?throwExceptionOnFailure=false
,
在第一种情况下,它可以工作,您已经有一个 requestpatam(
?action=myaction
),因此可以使用与号 (&)

添加下一个请求

1
投票

我认为您必须添加自己的逻辑来在运行时将端点组合到

http
组件。这是因为
CamelContext
会在路线本身期间对其进行处理。参数
throwExceptionOnFailure
http
组件的属性。

我认为通过

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

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

您可以使用简单表达式编写一个逻辑,根据

pushProperties.getUrl()
的结果执行您想要的操作。


0
投票

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

我的建议是创建一个配置映射,并将参数附加到其中,并使用“&”进行手动连接,然后将其附加到主网址。

我这样做:

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

© www.soinside.com 2019 - 2024. All rights reserved.