简体   繁体   中英

Camel http using ${body} in url

I am completely new to Camel. I am trying to follow a simple example how to consume a rest service.

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);
    }

}

When I run this test I get

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)

Somehow the ${body} seems not to be replaced with the expected String.. When I hardcode the parameter "USA" into the URL the request works as expected. Any ideas what my mistake might be? Thanks.

使用toD动态

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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