首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring集成与Spring:@Controller与带有服务激活器的入站网关

Spring集成与Spring:@Controller与带有服务激活器的入站网关
EN

Stack Overflow用户
提问于 2016-07-28 14:30:40
回答 1查看 1.6K关注 0票数 1

我想找到最好的方法来管理入站请求,使用、Spring、Spring

我有一个<int-http:inbound-gateway>,配置如下:

代码语言:javascript
复制
<!-- 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类:

代码语言:javascript
复制
public class ChannelService {

    public void manage(Message<?> message){

        // how to get the HttpServletRequest request here ???
    }

}

它的工作原理是:正确执行"manage()“方法,并且消息包含正确的有效负载。但是有一个小问题:,我在ServiceChannel的输入中没有任何关于HttpServletRequest的引用。

如果我使用Spring的@Controller,我可以使用相对的@RequestMapping处理每个请求。如果我想读取请求中包含的有效负载,我必须从inputStream of HttpServletRequest读取它。在任何地方,我都没有机会在频道中传递消息:

代码语言:javascript
复制
@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 ???

    }

}

如果我同时使用 (@Controllerinbound-gateway),那么@Controller映射将赢得入站网关的胜利:如果存在映射相同路径URI的@Controller,则没有机会使用inbound-gateway处理servlet路径URI。

因此,我想了解是否有某种方式在@Controller中使用Message<?> message,或者在ServiceActivator中使用HttpServletRequest,或者有其他方法来管理这个场景。

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-28 14:56:33

您可以通过HttpServletRequest ( MessageHeadersexpressions)访问该系统。

HttpRequestHandlingEndpointSupport的逻辑如下:

代码语言:javascript
复制
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>

代码语言:javascript
复制
<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"/>

requestAttributesRequestAttributes的一个实现。标准的一个是ServletRequestAttributes,在这里您可以找到getRequest()方法。是的,在表达式中也要用到它:

代码语言:javascript
复制
<int-http:header name="request" expression="#requestAttributes.request"/>

另一方面,您总是可以在自己的代码中使用RequestContextHolder.currentRequestAttributes(),因为它与ThreadLocal绑定在一起。

Spring对Spring一无所知,因此没有任何Message处理。

不管怎样,你也可以走那条路。为此,您应该引入@MessagingGateway,并从您的@Controller中委托一个逻辑。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38639362

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档