有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java骆驼如何将请求参数(ThroweExceptionOnFailure)添加到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

在camel中是否有向URL添加请求参数的通用方法

比如:

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

骆驼内部api


共 (2) 个答案

  1. # 1 楼答案

    这是因为在localhost:8080/url的情况下,在附加它之后会变成这样

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

  2. # 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()的结果执行所需操作