Camel CXF Service With Multiple Query Parameters

While the awesome Apache Camel team is busy fixing the handling of the multiple parameters in the query, here’s a workaround. Hopefully, this post will become obsolete with the next versions of Camel. (Currently, I use 2.7.5)


Query parameters more than 1 is passed as a null value into a Camel-CXF service. Say, if the URL has four query parameters as in


only the first one gets populated when you do a:


public String sourceResultsFromTwoSources(@QueryParam("name") String name, @QueryParam("age") String age,
                                          @QueryParam("phone") String phone,@QueryParam("email") String email

All other parameters are null.

Final Output

For url: http://localhost:8181/cxf/karafcxfcamel/search?name=arun& the result expected is :



Interestingly, we could get the entire query string in the header.


String queryString = exchange.getIn().getHeader(Exchange.HTTP_QUERY, String.class);

We could then do a


MultivaluedMap<String, String> queryMap = JAXRSUtils.getStructuredParams(queryString, "&", false, false);

to get the query parameters as a multi valued Map.

The query parameters could then be set as a property to the Exchange and used across the exchange.


  • The entire code could be downloaded from github

Please note that I am running Camel as part of OSGi inside the Karaf container. While the workaround does not differ because of the environment in which you are using Camel-CXF, please be wary of this fact when you download the code from github. Watch out for the blueprint xmls for Camel configuration.

The most important piece here is the Router



package me.rerun.karafcxfcamel.camel.beans;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.dataformat.JsonLibrary;
import org.apache.cxf.jaxrs.utils.JAXRSUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

public class RestToBeanRouter extends RouteBuilder {

    private static Logger logger= LoggerFactory.getLogger(RouteBuilder.class);

    public void configure() throws Exception {

        from ("cxfrs://bean://rsServer")
                .process(new ParamProcessor())
                .aggregationStrategy(new ResultAggregator())
                .beanRef("restServiceImpl", "getNameEmailResult")
                .beanRef("restServiceImpl", "getAgePhoneResult")

    private class ParamProcessor implements Processor {

        public void process(Exchange exchange) throws Exception {
            String queryString = exchange.getIn().getHeader(Exchange.HTTP_QUERY, String.class);

            MultivaluedMap<String, String> queryMap = JAXRSUtils.getStructuredParams(queryString, "&", false, false);

            for (Map.Entry<String, List<String>> eachQueryParam : queryMap.entrySet()) {
                exchange.setProperty(eachQueryParam.getKey(), eachQueryParam.getValue());







public interface RestService {

    public String sourceResultsFromTwoSources();





import me.rerun.karafcxfcamel.model.AgePhoneResult;
import me.rerun.karafcxfcamel.model.NameEmailResult;
import me.rerun.karafcxfcamel.service.base.AgePhoneService;
import me.rerun.karafcxfcamel.service.base.NameEmailService;
import me.rerun.karafcxfcamel.service.impl.AgePhoneServiceImpl;
import org.apache.camel.Exchange;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class RestServiceImpl implements RestService {

    private static Logger logger= LoggerFactory.getLogger(AgePhoneServiceImpl.class);

    private NameEmailService nameEmailService;
    private AgePhoneService agePhoneService;

    public RestServiceImpl(){

    //Do nothing. Camel intercepts and routes the requests
    public String sourceResultsFromTwoSources() {
        return null;

    public NameEmailResult getNameEmailResult(Exchange exchange){"Invoking getNameEmailResult from RestServiceImpl");

        String name=getFirstEntrySafelyFromList(exchange.getProperty("name", List.class));
        String email=getFirstEntrySafelyFromList(exchange.getProperty("email", List.class));

        return nameEmailService.getNameAndEmail(name, email);

    public AgePhoneResult getAgePhoneResult(Exchange exchange){"Invoking getAgePhoneResult from RestServiceImpl");

        String age=getFirstEntrySafelyFromList(exchange.getProperty("age", List.class));
        String phone=getFirstEntrySafelyFromList(exchange.getProperty("phone", List.class));

        return agePhoneService.getAgePhoneResult(age, phone);

    public NameEmailService getNameEmailService() {
        return nameEmailService;

    public AgePhoneService getAgePhoneService() {
        return agePhoneService;

    public void setNameEmailService(NameEmailService nameEmailService) {
        this.nameEmailService = nameEmailService;

    public void setAgePhoneService(AgePhoneService agePhoneService) {
        this.agePhoneService = agePhoneService;

    private String getFirstEntrySafelyFromList(List<String> list){

        if (list!=null && !list.isEmpty()){
            return list.get(0);
        return null;



Reference: Camel CXF Service With Multiple Query Parameters from our JCG partner Arun Manivannan at the blog.
Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply

2 − = zero

Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books
Get tutored by the Geeks! JCG Academy is a fact... Join Now
Hello. Add your message here.