Apache Camel – How to remove some or all exchange headers

While dealing with Apache Camel, we normally comes with scenarios where we have to send only message body, but exchange header have lots of unwanted data. In this case we can remove all headers in one single method call.

from(fromEndPoint)
.removeHeaders("*")
.to(toEndPoint)

In some other scenarios, we have to remove all headers except few. This can also achieved by using below code:

from(fromEndPoint)
.removeHeaders("*", "RequiredHeader1", "RequiredHeader2", "RequiredHeader3")
.to(toEndPoint)

With the above code, it will remove all the headers from exchange, but will keep header values for “RequiredHeader1”, “RequiredHeader2” and “RequiredHeader3”.

Leave a Reply

Your email address will not be published. Required fields are marked *