I have an inbound http gateway whose incoming message is passed to an outbound http gateway (using integration java dsl).
When the outbound gateway response has http status 200, the outbound response goes back to the caller of the inbound gateway as expected.
But when the outbound gateway responds with specific client errors like 423 locked or 403 forbidden, the caller of the inbound gateway receives 500 and a body message that contains an exception as String.
I understand that this is because SI handles outbound http errors as exceptions. But in my case I need to pass through the error status and the response body that comes with the outbound response back to the caller of the inbound gateway, for the specific error codes. How do I do that?
We call this scenario as HTTP Proxy and have some test-case on the matter: https://github.com/spring-projects/spring-integration/blob/master/spring-integration-http/src/test/java/org/springframework/integration/http/dsl/HttpDslTests.java
I've just done some investigation and here is what you need:
We have to handle downstream errors on the Inbound Gateway level. For this purpose Spring Integration suggest the
errorChannel
functionality.That
httpProxyErrorFlow
provides some logic on the matter. First of all we know that the message for theerrorChannel
isErrorMessage
and itspayload
isMessagingException
- as a result of wrappingHttpClientErrorException
in theHttpRequestExecutingMessageHandler
. So, with the.transform()
we drill down to the desired exception and in the.handle()
we build a new message based on theHttpClientErrorException
content.The
HttpRequestHandlingMessagingGateway
is able to process such a message properly and set a desired status code to the response:http://docs.spring.io/spring-integration/docs/4.3.11.RELEASE/reference/html/http.html#http-response-statuscode