Search code examples
javarestapache-camel

apache camel rest dsl parameter passing


I want to get whether information by city via apache camel weather api. I can get whether information from api but when I want to pass cirt and country information to rest api I got error.

I couldnt find any example that shows how to pass a header information from consumer to processor.

Here is my code

  @Override
public void configure() throws Exception {

    rest("/api").description("Weather api")
            .consumes(MediaType.APPLICATION_JSON_VALUE)
            .produces(MediaType.APPLICATION_JSON_VALUE)
            .get("/weather").description("Get weather by city")
            .param().name("city").type(RestParamType.header).description("The city name").dataType("string").endParam()
            .param().name("country").type(RestParamType.header).description("The country name").dataType("string").endParam()
            .to("direct:weather");

    from("direct:weather")
            .log("Get weather by city")
            .to("weather:foo?location=${header.city},${header.country}&appid=8bb3bc2e4efb92992ed3e7190&units=metric")
            .log("Weather: ${body}");
}

as you can see I got 2 parameters in rest api and I want to use these parameters in "from" part to pass these parameters to my camel weather api

have any idea


Solution

  • U must use 'toD' (to Dynamic )instead of 'to'

    .toD("weather:foo?location=${header.city},${header.country}&appid=8bb3bc2e4efb92992ed3e7190&units=metric")