【问题标题】:HttpMessageConverter error handling in inbound gateway入站网关中的 HttpMessageConverter 错误处理
【发布时间】:2016-11-22 04:01:30
【问题描述】:

当我在入站网关中使用消息转换器发生转换错误时,我遇到了一种奇怪的行为。下例中的想法是接收 XML 有效负载(或序列化的 java),将它们转换为 java 对象并使用相同的媒体类型进行响应。

鉴于此配置:

<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>

如果提交了无效的 XML 有效负载(例如没有结束标记),则在 JAXB XML 转换器中引发的异常 HttpMessageNotReadableException 不会在 errorChannel 中转发(我在其中定义了一个服务激活器来处理异常)。请注意,此处理程序有效负载转换后运行良好。

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

我在这里缺少什么?为什么我的错误处理程序没有处理HttpMessageNotReadableException?欢迎任何帮助!

【问题讨论】:

    标签: java xml spring jaxb spring-integration


    【解决方案1】:

    为什么我的错误处理程序没有处理 HttpMessageNotReadableException?

    error-channel只有在我们已经发送Message到下游流时才会生效,但是如果你传入的HTTP请求不能转换为Message,那么仍然没有任何东西可以发送到error-channel .

    是的,对于convertExceptions = true,我们不能只返回一个异常并让一些HttpMessageConverter 将其转换为合理的HTTP 响应。通常SerializingHttpMessageConverter 在现场。

    为什么这个异常没有包装在 MessageHandlingException 中并发送到我的错误处理程序?

    只是因为我们还没有收到消息。对于您的问题,我们仍然处于标准的 Spring MVC 环境中,并且 Spring Integration 在请求转换期间仍然无能为力。

    您应该为您的案例考虑 Spring MVC 的一些解决方案:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-exceptionhandlers

    【讨论】:

      【解决方案2】:

      convert-exceptions="true" 添加到您的int-http:inbound-gateway 可以解决异常流问题。这个post 对我很有用。

      【讨论】:

      • 它似乎做了一些事情,因为异常现在写在响应中。但是这种异常没有转换器。我现在有一个 MessagingException:Could not convert reply: no suitable HttpMessageConverter found for type [org.springframework.http.converter.HttpMessageConversionException]。我找到的唯一解决方案是为这种异常创建一个特定的转换器。为什么此异常没有包含在 MessageHandlingException 中并发送到我的错误处理程序?
      • @angcap, convert-exceptions 指定view-name 无效。在这种情况下,&lt;int-http:inbound-gateway&gt; 就像普通的@Controller 一样工作。见convert-exceptionsXSD属性说明。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-26
      • 1970-01-01
      • 2021-01-25
      • 1970-01-01
      相关资源
      最近更新 更多