【问题标题】:Camel http using ${body} in url骆驼 http 在 url 中使用 ${body}
【发布时间】:2019-02-28 17:13:33
【问题描述】:

我对 Camel 完全陌生。我正在尝试通过一个简单的示例来使用休息服务。

public class RestRoute extends RouteBuilder {

    @Override
    public void configure() throws Exception {

        getContext().getProperties().put("http.proxyHost", "myproxy");
        getContext().getProperties().put("http.proxyPort","8080");

        from("direct:restCall")
        .to("log:DEBUG?showBody=true&showHeaders=true")

        .setHeader(Exchange.HTTP_METHOD, constant("GET"))
        .setHeader(Exchange.HTTP_BASE_URI, simple("http://restcountries.eu/rest/v2/alpha/${body}"))
        .to("http://restcountries.eu/rest/v2/alpha/${body}")
        .to("log:?level=INFO&showBody=true");

    }
}

public class RestRouteTest extends CamelTestSupport {

    @Override
    public RouteBuilder createRouteBuilder() {
        return new RestRoute();
    }

    @Test
    public void test() {
        String response =   template.requestBody("direct:restCall","USA",String.class);
        System.out.println("response : " + response);
        assertNotNull(response);
    }

}

当我运行这个测试时,我得到了

08:01| WARN | HttpMethodBase.java 1656 | Cookie rejected: "$Version=0; __cfduid=d05edb152ec46afbcb3763a2e231b959b1551337302; $Path=/; $Domain=.restcountries.eu". Illegal domain attribute ".restcountries.eu". Domain of origin: "restcountries.eu"
08:01| ERROR | CamelLogger.java 204 | Failed delivery for (MessageId: ID-DZS10W321-1551337302436-0-2 on ExchangeId: ID-DZS10W321-1551337302436-0-1). Exhausted after delivery attempt: 1 caught: org.apache.camel.http.common.HttpOperationFailedException: HTTP operation failed invoking http://restcountries.eu/rest/v2/alpha/$%7Bbody%7D with statusCode: 400

Message History
---------------------------------------------------------------------------------------------------------------------------------------
RouteId              ProcessorId          Processor                                                                        Elapsed (ms)
[route1            ] [route1            ] [direct://restCall                                                             ] [       317]
[route1            ] [to1               ] [log:DEBUG?showBody=true&showHeaders=true                                      ] [         3]
[route1            ] [setHeader1        ] [setHeader[CamelHttpMethod]                                                    ] [         0]
[route1            ] [setHeader2        ] [setHeader[CamelHttpBaseUri]                                                   ] [         6]
[route1            ] [to2               ] [http://restcountries.eu/rest/v2/alpha/${body}                                 ] [       306]

Stacktrace
---------------------------------------------------------------------------------------------------------------------------------------
org.apache.camel.http.common.HttpOperationFailedException: HTTP operation failed invoking http://restcountries.eu/rest/v2/alpha/$%7Bbody%7D with statusCode: 400
    at org.apache.camel.component.http.HttpProducer.populateHttpOperationFailedException(HttpProducer.java:243)
    at org.apache.camel.component.http.HttpProducer.process(HttpProducer.java:165)

不知何故,${body} 似乎没有被预期的字符串替换.. 当我将参数“USA”硬编码到 URL 中时,请求按预期工作。 任何想法我的错误可能是什么? 谢谢。

【问题讨论】:

    标签: http apache-camel


    【解决方案1】:

    使用toD 进行动态处理

     .toD("http://restcountries.eu/rest/v2/alpha/${body}")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-10
      • 1970-01-01
      • 1970-01-01
      • 2013-12-22
      • 2016-02-21
      • 1970-01-01
      • 1970-01-01
      • 2022-01-11
      相关资源
      最近更新 更多