簡體   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