【问题标题】:Proxy a RESTful service using SOAP with WSO2 ESB使用带有 WSO2 ESB 的 SOAP 代理 RESTful 服务
【发布时间】:2012-12-08 21:36:46
【问题描述】:

我们想用 SOAP 代理一个 RESTful Web 服务。

REST 服务使用 GET 方法并通过查询参数接受输入。它会生成 application/csv 类型的资源。

WSO2 ESB/Synapse 是否支持这样的场景,是否有可用的示例?

示例请求

SOAP 代理请求:

<request>
  <fromDate>2012-01-01</fromDate>
  <toDate>2012-12-31</toDate>
</request>

REST 端点请求:

http://localhost/person?fromDate=2012-01-01&toDate=2012-12-31

示例响应

REST 端点响应

"Name","Age","Sex"
"Geoff","22","Male"

SOAP 代理响应

<person>
  <name>Geoff</name>
  <age>22</age>
  <sex>Male</sex>
<person>

非常感谢。

【问题讨论】:

    标签: rest wso2 wso2esb


    【解决方案1】:

    如果我理解正确,您希望将 REST 服务公开为 SOAP 服务,以便 SOAP 客户端可以通过 ESB 访问您的 REST 服务?

    如果是这种情况,它是可能的 :) 你应该从这些检查样本 152:http://docs.wso2.org/wiki/display/ESB451/Proxy+Service+Samples

    它将解释如何接收 SOAP 请求并将其传递到 REST 后端,然后将 REST 响应转换为 SOAP 响应。

    编辑:这是关于如何执行您在 cmets 中询问的示例配置,希望它可以帮助您入门:)

    <proxy xmlns="http://ws.apache.org/ns/synapse" name="RESTProxy" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
       <target>
          <inSequence>
    
             <!-- We set the HTTP Method we want to use to make the REST request here -->
             <property name="HTTP_METHOD" value="GET" scope="axis2"/>
    
             <!-- This is where the magic happens, for what you want i.e. mapping SOAP "params" to REST query param's --> 
             <property name="messageType" value="application/x-www-form-urlencoded" scope="axis2"/>
    
             <send>
                <endpoint>
                   <!-- This is the RESTful URL we are going to query, like the one in the ESB example 152 -->
                   <address uri="http://localhost/person" />
                </endpoint>
             </send>
    
          </inSequence>
    
          <outSequence>
             <log level="full"/>
             <property name="messageType" value="text/xml" scope="axis2"/>
             <send/>
          </outSequence>
       </target>
       <description></description>
    </proxy>
    

    那么您向 ESB 发出的 SOAP 请求应该类似于:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
       <soapenv:Header/>
       <soapenv:Body>
        <person>
            <fromDate>2012-01-01</fromDate>
            <toDate>2012-12-31</toDate>
        </person>
       </soapenv:Body>
    </soapenv:Envelope>
    

    希望对你有帮助:)

    【讨论】:

    • 非常感谢,这个例子很有帮助。但是我们仍然想知道 - 我们如何将 SOAP xml 参数转换为 REST 调用的查询参数?你能帮忙吗?
    【解决方案2】:

    您可以使用类中介器通过 XPATH 提取 SOAP 参数。然后构建 REST URL 并将其发送回 IN 序列流。

    【讨论】:

      【解决方案3】:

      【讨论】:

        【解决方案4】:

        1. 你需要从 SOAP PROXY 中获取值

        2. 你需要将它存储在一个局部变量中

        3. 您需要使用查询参数将值传递给 REST SERVICE

        4. 您需要将来自 REST 服务的响应格式化为 SOAP 格式

        SOAP 请求将是,

        <request> <fromDate>2012-01-01</fromDate> <toDate>2012-12-31</toDate> </request>

        您可以将来自 SOAP PROXY 请求的值存储为,

        <proxy xmlns="http://ws.apache.org/ns/synapse" name="RESTProxy" transports="https,http" statistics="disable" trace="disable" startOnLoad="true><target>
          <inSequence>
              <property name="fromDate" expression="//fromDate" scope="default" type="STRING"/>
              <property name="toDate" expression="//toDate" scope="default" type="STRING"/>
        

        然后您可以通过以下方式将值传递给 REST 服务,

        <send>
             <endpoint>  
                   <http method="GET" uri-template="http://localhost/person?fromDate=={get-property('fromDate')}&amp;toDate={get-property('toDate')}"/>  
            </endpoint>
        </send>  
        </inSequence>
        

        然后您可以使用 PayloadFactory mediator 格式化响应,

        <outSequence>  
        <payloadFactory media-type="xml">     
              <format>
                       <person>
                              <Name>$1</Name>
                              <Age>$2</Age>
                              <Sex>$3</Sex>
                        </person>
                </format>
                        <args>
                            <arg evaluator="json" expression="$.Name"/>
                            <arg evaluator="json" expression="$.Age"/>
                            <arg evaluator="json" expression="$.Sex"/>
                        </args>
         </payloadFactory>
             <send/>
          </outSequence>
           </target>
           <description/>
          </proxy>
        

        所以代理的响应将是,

        <person>
          <name>Geoff</name>
          <age>22</age>
          <sex>Male</sex>
        <person>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-06-15
          • 2012-08-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多