我想找到最好的方法来管理入站请求,使用、Spring、和Spring 。
我有一个<int-http:inbound-gateway>,配置如下:
<!-- 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"/>通过这种集成,在路径URI:/service/**上进行的每个HTTP调用都在ChannelService类的ChannelService方法中处理。
这是ChannelService类:
public class ChannelService {
public void manage(Message<?> message){
// how to get the HttpServletRequest request here ???
}
}它的工作原理是:正确执行"manage()“方法,并且消息包含正确的有效负载。但是有一个小问题:,我在ServiceChannel的输入中没有任何关于HttpServletRequest的引用。
如果我使用Spring的@Controller,我可以使用相对的@RequestMapping处理每个请求。如果我想读取请求中包含的有效负载,我必须从inputStream of HttpServletRequest读取它。在任何地方,我都没有机会在频道中传递消息:
@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 ???
}
}如果我同时使用 (@Controller和inbound-gateway),那么@Controller映射将赢得入站网关的胜利:如果存在映射相同路径URI的@Controller,则没有机会使用inbound-gateway处理servlet路径URI。
因此,我想了解是否有某种方式在@Controller中使用Message<?> message,或者在ServiceActivator中使用HttpServletRequest,或者有其他方法来管理这个场景。
提前谢谢。
发布于 2016-07-28 14:56:33
您可以通过HttpServletRequest ( MessageHeaders,expressions)访问该系统。
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());因此,您可以使用以下子元素配置您的<int-http:inbound-gateway>:
<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"/>requestAttributes是RequestAttributes的一个实现。标准的一个是ServletRequestAttributes,在这里您可以找到getRequest()方法。是的,在表达式中也要用到它:
<int-http:header name="request" expression="#requestAttributes.request"/>另一方面,您总是可以在自己的代码中使用RequestContextHolder.currentRequestAttributes(),因为它与ThreadLocal绑定在一起。
Spring对Spring一无所知,因此没有任何Message处理。
不管怎样,你也可以走那条路。为此,您应该引入@MessagingGateway,并从您的@Controller中委托一个逻辑。
https://stackoverflow.com/questions/38639362
复制相似问题