Spring集成和Spring MVC:@Controller vs带有服务激活器的入站网关
我想找到使用Spring集成HTTP和Spring MVC管理入站请求的最佳方法 我的Spring集成和Spring MVC:@Controller vs带有服务激活器的入站网关,spring,spring-mvc,spring-integration,Spring,Spring Mvc,Spring Integration,我想找到使用Spring集成HTTP和Spring MVC管理入站请求的最佳方法 我的配置如下: <!-- CHANNEL --> <int:channel id="requestChannel"> <int:dispatcher task-executor="executor"/> </int:channel> <int:channel id="outputChannel" /> <!-- INBOUND GATEWA
配置如下:
<!-- CHANNEL -->
<int:channel id="requestChannel">
<int:dispatcher task-executor="executor"/>
</int:channel>
<int:channel id="outputChannel" />
<!-- INBOUND GATEWAY -->
<int-http:inbound-gateway id="gateway" request-channel="requestChannel"
path="/service/**"
supported-methods="POST"
reply-channel="outputChannel"
header-mapper="headerMapper">
</int-http:inbound-gateway>
<!-- SERVICE ACTIVATOR -->
<int:service-activator id="channelServiceActivator"
ref="channelService"
input-channel="requestChannel"
output-channel="outputChannel"
method="manage"/>
<bean id="channelService"
class="test.spring.data.rest.xml.channel.ChannelService"/>
它可以工作:正确执行“manage()”方法,并且消息包含正确的负载。
但是有一个小问题:我没有任何关于在该ServiceChannel的输入中接收到的HttpServletRequest的引用
如果我使用SpringMVC的@Controller
,我可以用相对的@RequestMapping
处理每个请求。
如果我想读取请求中包含的有效负载,我必须从HttpServletRequest的inputStream中读取它。无论在何处,我都没有机会将消息传递到频道:
@Controller
@RequestMapping(value="/service")
public class ServiceController {
@RequestMapping(value="/**")
public handle(HttpServletRequest request) throws Exception{
// how to get the Message<?> message passed on the channel here ???
}
}
@控制器
@请求映射(value=“/service”)
公共类服务控制器{
@请求映射(值=“/**”)
公共句柄(HttpServletRequest)引发异常{
//如何在这里的频道上传递消息???
}
}
如果我同时使用(@Controller
和入站网关
),则@Controller映射将胜过入站网关:如果存在入站网关,则没有机会处理servlet路径URI
映射相同路径URI的@控制器
因此,我想了解是否有某种方法可以在@Controller中包含消息
,或者在ServiceActivator中包含HttpServletRequest,或者其他方法来管理此场景
提前感谢。您可以通过
MessageHeaders
()访问HttpServletRequest
HttpRequestHandlingEndpointSupport的逻辑如下:
evaluationContext.setVariable("requestAttributes", RequestContextHolder.currentRequestAttributes());
MultiValueMap<String, String> requestParams = this.convertParameterMap(servletRequest.getParameterMap());
evaluationContext.setVariable("requestParams", requestParams);
evaluationContext.setVariable("requestHeaders", new ServletServerHttpRequest(servletRequest).getHeaders());
另一方面,您始终可以在自己的代码中使用RequestContextHolder.currentRequestAttributes()
,因为它与ThreadLocal
绑定
SpringMVC对Spring集成一无所知,因此没有任何消息处理
不管怎样,你也可以走那条路。为此,您应该介绍@MessagingGateway
,并从您的@Controller
委托一个逻辑,非常感谢您的精彩解释。
evaluationContext.setVariable("requestAttributes", RequestContextHolder.currentRequestAttributes());
MultiValueMap<String, String> requestParams = this.convertParameterMap(servletRequest.getParameterMap());
evaluationContext.setVariable("requestParams", requestParams);
evaluationContext.setVariable("requestHeaders", new ServletServerHttpRequest(servletRequest).getHeaders());
<int-http:header name="requestAttributes" expression="#requestAttributes"/>
<int-http:header name="requestParams" expression="#requestParams"/>
<int-http:header name="requestHeaders" expression="#requestHeaders"/>
<int-http:header name="matrixVariables" expression="#matrixVariables"/>
<int-http:header name="cookies" expression="#cookies"/>
<int-http:header name="request" expression="#requestAttributes.request"/>