Search code examples
javaapache-cameljavabeans

Apache camel getbody as custom class


The question is rather simple, perhaps because I'm a little confused in the process. What I'm trying to do is shown in the code example:

cc.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("file://files?noop=true")
                    .split()
                    .tokenize("\n")
                    .split()
                    .method(SplitToken.class, "hashTokens")

and:

class SplitToken {
@SuppressWarnings("unchecked")
public static List<HashMap<String, Integer>> hashTokens(final Exchange exchange) {
    List<String> oldstr = exchange.getIn().getBody(List<String>);
    //Create a key value hashmap from accumulated string list
    }
}

but returns error:

expression required.

Any ideas about how we can achieve getbody with desired class in general? (Since the first split method returns a string list but I can't retrieve it in second split, or can I?)


Solution

  • As far as I remember you cannot get generic list with getBody. This might work:

    List<String> oldstr = (List<String>)exchange.getIn().getBody(List.class);
    

    or even better you can make camel extract body for you with @Body annotation:

    public static List<HashMap<String, Integer>> hashTokens(final Exchange exchange, @Body List<String> oldStr) {
        //Create a key value hashmap from accumulated string list
        return new ArrayList<>();
    }