2016-07-19 29 views
0

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

鉴于这种配置:

<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转发(其中I所定义的服务激活器来处理异常)。请注意,此处理程序在之后效果良好负载转换。

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

我在这里错过了什么?为什么我的错误处理程序不能处理HttpMessageNotReadableException?欢迎任何帮助!

回答

2

为什么HttpMessageNotReadableException不由我的错误处理程序处理?

error-channel生效,只有当我们已经发送Message向下游流动,但如果你进入的HTTP请求无法转换为Message,还有什么可发送到error-channel

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

为什么这个异常不包含在MessageHandlingException中并发送给我的错误处理程序?

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

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

0

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

+0

似乎做一些事情的异常现在写的响应。但是这种例外没有转换器。 我现在有一个MessagingException:“无法转换回复:找不到适用于[org.springframework.http.converter.HttpMessageConversionException]类型的HttpMessageConverter'。我发现的唯一解决方案是为这种异常创建特定的转换器。为什么这个异常不包含在'MessageHandlingException'中并发送给我的错误处理程序? –

+0

@angcap,如果指定'view-name','convert-exceptions'不起作用。在这种情况下,''像普通的'@ Controller'一样工作。请参阅'convert-exceptions' XSD属性说明。 –