0

I am trying to build an integration solution where

IntegrationFlows
  .from(inBoundGateway)
  .enrichHeaders(enrichHeaders())
  .transform(dto to externaldto)
  .handle(outBoundGateway, advice -> advice.advice(retryAdvice()))
  .transform(exetrnaldto to dto)
  .get()

  @Bean
  RequestHandlerRetryAdvice rhra
  rhra.setRecoveryCalBack(errorMessgaeRecoverer());

  @Bean
  ErrorMessageSendingRecoverer errorMessgaeRecoverer

and my outboundgateway is defined as

Http.outboundGateway(uri, resttemplate)
 ...
 .get()


new RestTemplate(requestFactory)

where requestFactory is

TrustStratgey ts = new TrustStratgey(){
 public boolean isTrusted(...){
   return true;
 }
}
SSLContext context = SSLContexts,custom().loaddTrustMaterials(null, ts);
SSLContextFactory cf = new SSLContextFactory(context, new NoopHostnameVerifier());
HttpClientBuilder clientBuilder ..
clientBuilder.setSSLSocketFactory()

Happy path works fine, the problem i am facing is with not so happy path.

  • When Api call returns Error response .transform(exetrnaldto to dto) fails and client get 500
  • I want to translate error resposne json as well to my json
  • How do i handle error situations.

My questions are;

  1. How to handle errors.
  2. In error conditions how to stop flow not to transform
  3. How to send status code in response from outbound to client (this is important)
  4. How to handle error like typical @Controller Advice @ErrorHandler mechanism or similar.

Hope Garry get to see this post, couldnt find any answers, i looked through many books and forums, feels like Java DSL is not widely used or commented yet.

Zafar Ali
  • 37
  • 1
  • 8

1 Answers1

0

There is nothing Spring Integration Java DSL specific in your question. It is really more about how to handle HTTP error on the server side (MVC - HttpInboundGateway) and on the client side - RestTemplate and HttpOutboundGateway. The Java DSL is just a syntax sugar over existing technologies and solutions.

So, the @Controller Advice @ErrorHandler is an MVC, server side. You definitely can make it working with the HttpInboundGateway, but it is indeed wrong direction for the client side REST calls.

It looks like you would like to catch a REST error call for that your .handle(outBoundGateway). Consider to use an ExpressionEvaluatingRequestHandlerAdvice along side with that retryAdvice. So, you will be able with its failureChannel to handle an exception and transform it into a desired JSON respectively.

See more in docs: https://docs.spring.io/spring-integration/docs/current/reference/html/messaging-endpoints.html#expression-advice

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118