Search code examples
javaspringspring-integrationspring-integration-http

Spring Integration - Adding custom headers in Message


I am using Spring Integration filter to do a structural validation of the incoming payload and if the validation fails then i want to add some custom headers to the original message.

The filter code is below :

@Service("structureValidationFilter")
public class StructureValidationFilter implements MessageSelector {

@Override
public boolean accept(Message<?> message) {
    // TODO Auto-generated method stub
    boolean status=true;

    if(message.getPayload() instanceof CFKRequestBody) {
        CFKRequestBody body=(CFKRequestBody)message.getPayload();
        if(!body.getInitiatingPartyId().equalsIgnoreCase("BPKV")) {
            message = MutableMessageBuilder.fromMessage(message).
                    setHeader("BPKV_ERROR_CODE", "Ïnvalid Initiating part id").
                    setHeader("HTTP_STATUS", "400").build();

            return false;   
        }

    }
    return status;
}

}

But the headers are not populating in the Message. Not able to see the headers added in the next component. What am i doing wrong here.


Solution

  • You can't replace a parameter and expect it to be propagated to the next component; Java doesn't work that way; your new message is simply discarded.

    Use a service activator instead of a filter and return the new message, or null which is a signal to end the flow at that point.