Quentin Lebourgeois
Quentin Lebourgeois

Reputation: 3

HttpMessageConverter error handling in inbound gateway

I'm facing a weird behavior when a conversion error occurs using a message converter in my inbound gateway. The idea in the example below is to receive XML payloads (or serialized java), convert them to java objects and respond with the same media type.

Given this configuration:

<bean id="converterXml" class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter" />
<bean id="converterSerialized" class="org.springframework.integration.http.converter.SerializingHttpMessageConverter" />

<util:list id="converters">
  <ref bean="converterXml" />
  <ref bean="converterSerialized" />
</util:list>

<int-http:inbound-gateway id="inboundIntegrationGateway" 
  view-name="/integration"
  message-converters="converters" error-channel="errorChannel"
  request-payload-type="MyXmlPojo"
  supported-methods="POST" request-channel="inboundIntegration"
  path="/services/integration"
  reply-timeout="50000">
</int-http:inbound-gateway>

If an invalid XML payload (no end tag for example) is submitted, the exception HttpMessageNotReadableException raised in the JAXB XML converter is not forwarded in the errorChannel (where I defined a service activator to handle exceptions). Note that this handler works well after the payload conversion.

<int:service-activator input-channel="errorChannel" ref="exceptionHandlerService"
method="handleException" requires-reply="true" />

What am I missing here? Why is the HttpMessageNotReadableException not handled by my error handler? Any help is welcome!

Upvotes: 0

Views: 1533

Answers (2)

Artem Bilan
Artem Bilan

Reputation: 121177

Why is the HttpMessageNotReadableException not handled by my error handler?

The error-channel comes into force only when we already send Message to the downstream flow, but if your incoming HTTP request can't be converted to Message, there is still nothing to send to the error-channel.

Right, with convertExceptions = true we can't just return an exception as is and let some HttpMessageConverter to convert it into a reasonable HTTP response. Typically SerializingHttpMessageConverter is on the scene.

Why is this exception not wrapped in a MessageHandlingException and sent to my error handler?

Just because we haven't reached messaging yet. With your problem we are still in the standard Spring MVC environment and Spring Integration is still powerless here during request conversion.

You should consider some solution from Spring MVC for your case: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-exceptionhandlers

Upvotes: 2

angcap
angcap

Reputation: 169

Adding convert-exceptions="true" to your int-http:inbound-gatewaycould solve the exception flow problem. This post has been useful for me.

Upvotes: 0

Related Questions