如何使用Camel rest获取URL查询的Map?

时间:2019-04-03 09:11:47

标签: java rest apache-camel

如何配置Camel将所有URL查询参数通过bean方法传递给Map?

给出此示例代码

@ContextName("camelBean")
public class CamelTestRoute extends RouteBuilder {

    @Override
    public void configure() throws Exception {       
        rest("/foo").description("Test api")
            .consumes("application/json").produces("application/json")
            .get("/stuff").description("get stuff")
                .param().name("key").type(RestParamType.query).description("The key parameter").dataType("string").endParam()
                .responseMessage().code(200).message("The stuff").endResponseMessage()
                .to("bean:camelBean?method=getStuff")
            ;
    }
}

@Named("camelBean")
public class CamelServiceBean {

    public String getStuff(Map<String, String> args) {
        return String.valueOf(args);
    }

}

我访问<camelurl>/foo/stuff?key=asdasd时,希望通过包含getStuff的地图来调用'key' => 'asdasd'

1 个答案:

答案 0 :(得分:0)

您可以使用@Headers注释地图

http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/Headers.html

public String getStuff(@Headers Map<String, Object> args) {
    return String.valueOf(args);
}

args将以this map结尾。

请参见the documentation for annotations