How to add a Map to a query parameter in a Camel Endpoint

The Problem

In Camel, a String or Boolean is typically used as a query parameter. You can chain multiple query parameters together. For example in the following we have fileName and delete as query parameters.

Java
from("file:input?fileName=ABC&delete=true)

But what happens when you want a Map as a query parameter? E.g. for the Bean-Component, you have „parameters“ as a Map parameter. But how can you write this parameter in the Java-DSL?

Map Query Parameters for Consumer

Let’s first look at the consumer. Let’s say we have a „myMapParam“ as a String – String Map

Java
public class MyConsumerComponent extends DefaultComponent {
    @UriParam
    private Map<String, String> myMapParam;

    // ... other code ...

    public Map<String, String> getMyMapParam() {
        return myMapParam;
    }

    public void setMyMapParam(Map<String, String> myMapParam) {
        this.myMapParam = myMapParam;
    }
}

Now to add multiple values to this Map, we do it the following way:

Java
mycomponent://myendpoint?myMapParam.key1=value1&myMapParam.key2=value2

Map Query Parameters for Producers

Now let’s take a look at the producers

There we can also use the same technique as in the consumers part.

But Additionally, we can also use headers. But make sure this component also uses the headers!

Java
from("direct:test")
    .setHeader(MY_COMPONENT.MY_MAP_PARAM, constant(Collections.singletonMap("key1", "value1")))
    .to("mycomponent://myendpoint");

Kommentar verfassen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Nach oben scrollen