Search code examples
restspring-integrationspring-integration-http

How to handle 404 return code (by design) in int-http:outbound-gateway?


I code a Spring Integration route that queries a SOAP and a REST WS and somehow aggregates the results. I use a bean as payload to gather the useful information along the way, two payload enrichers to query the web services and a transformer at the end to build the result.

Now my problem is that the REST WS returns (by design) HttpStatus.NOT_FOUND (404) if the system has no information about the entity I'm querying. That needs be considered a nominal use case. Spring Integration considers this by default an error and bails out. I need to handle this case properly, store null in my bean or something similar (I can handle that down the road) and proceed.

So I've tried adding an error-handler to the outbound-gateway but could not make this work. If I tell this is not an error (overriding hasError()) then SI tries to unmarshall the body of the response, which obviously does not work; same if I override handleError() and do nothing.

Maybe I'm not going in the right direction but I miss a better idea so... any help accepted with gratitude.


Solution

  • You can add for the <int-http:outbound-gateway> a ExpressionEvaluatingRequestHandlerAdvice as a nested <request-handler-advice-chain> and use its options: onFailureExpression, returnFailureExpressionResult = true. You may also consider to use failureChannel.

    This way you catch that 404 NOT FOUND exception and return whatever you think is appropriate compensation result.

    More information you can find in the Reference Manual and there is also a sample project on the matter.