在网关中设置标头值

时间:2014-07-22 16:21:06

标签: spring-integration

以下是我目前的网络服务配置。对于每个服务,我创建一个单独的网关和标头扩充器,所以我可以添加一个标头,告诉路由器路由到哪个服务。为简洁起见,我省略了一些配置。我也明白这可以通过另一个流程完成 - 但是还有其他一些工作部分(比如版本控制,弃用等),所以我需要以这种方式设置它并在流程中稍后有一个自定义路由器。

<bean
        class="org.springframework.ws.server.endpoint.mapping.UriEndpointMapping">
        <property name="mappings">
            <props>
                <!-- TODO use config property for host -->
                <prop key="${service.endpoint.url}/SNTWS/service/CompanyService">SOAPCompanyGateway</prop>
                <prop key="${service.endpoint.url}/SNTWS/service/ContactService">SOAPContactGateway</prop>

            </props>
        </property>
    </bean>

    <!-- define gateways for each service endpoint -->

    <int-ws:inbound-gateway id="SOAPCompanyGateway"
        request-channel="SOAPCompanyRequestChannel" marshaller="SOAPMarshaller"
        unmarshaller="SOAPMarshaller" />

    <int-ws:inbound-gateway id="SOAPContactGateway"
        request-channel="SOAPContactRequestChannel" marshaller="SOAPMarshaller"
        unmarshaller="SOAPMarshaller" />

    <bean id="SOAPMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="contextPaths">
            <list>
                <!-- list all schema versions that we wish to accept -->
                <value>com.predictivesolutions.schema.v1_1</value>
                <value>com.predictivesolutions.schema.v2_0</value>
            </list>
        </property>
    </bean>


    <!-- these header enrichers add the service header and send all the messages 
        to the same output channel, add specific service headers in this section 
        before all messages are placed on a common channel. THe values of the header 
        must match the service-activator input-channel -->

    <int:header-enricher input-channel="SOAPContactRequestChannel"
        output-channel="SOAPRequestChannel">
        <int:header name="service" value="ContactService" />
    </int:header-enricher>

    <int:header-enricher input-channel="SOAPCompanyRequestChannel"
        output-channel="SOAPRequestChannel">
        <int:header name="service" value="CompanyService" />
    </int:header-enricher>

    <!-- set another header with the package name, which is used to determine 
        version. THis is necessary (along with service header) to route messages 
        to the appropriate versioned service. This code is the same regardless of 
        service type. -->
    <int:header-enricher input-channel="SOAPRequestChannel"
        output-channel="SOAPRequestChannelWithHeaders">
        <int:header name="version"
            expression="payload.getClass().getPackage().getName().substring(payload.getClass().getPackage().getName().lastIndexOf('.')+1)" />
    </int:header-enricher>

我需要单独的网关和标头扩展的原因是我可以在消息上设置适当的服务标头。所有消息都将放在SOAPRequestChannel上,我将获得该版本。

理想情况下,为了减少配置,我希望为所有端点映射都有一个网关。我可以从URL设置标题,因为我知道最后一部分是服务名称。

我正在寻找这样做的想法。我可以使用Spel访问URL路径吗?

1 个答案:

答案 0 :(得分:1)

是的,你可以使用URI

来做到这一点
<int:header-enricher>
    <int:header name="uri" 
      expression="T(org.springframework.ws.transport.context.TransportContextHolder).transportContext.connection.uri" />
</int:header-enricher>

拥有你可以根据uri值进行路由,甚至可以分别删除这两个<int-ws:inbound-gateway><int:header-enricher>