【问题标题】:Getting data elements from SOAP request从 SOAP 请求中获取数据元素
【发布时间】:2013-01-25 15:39:16
【问题描述】:

我的请求如下

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"     xmlns:hel="http://example.org/HelloService">
   <soapenv:Header>
<sampler>Test</sampler>     
   </soapenv:Header>
   <soapenv:Body>
  <hel:LastName>
     <lastName>Test</lastName>
  </hel:LastName>
   </soapenv:Body>
</soapenv:Envelope>

暴露我的 CXF:proxy-service 的流程如下

<flow name="WS_In">
    <http:inbound-endpoint address="http://localhost:8080/HelloService"
        exchange-pattern="request-response">            
        <cxf:proxy-service wsdlLocation="classpath:helloservice.wsdl"
            namespace="http://example.org/HelloService" service="ProxyService" >
            <cxf:inInterceptors>
            <spring:bean id="inLogger"  
                class="org.apache.cxf.interceptor.LoggingInInterceptor" />  
            <spring:bean id="msgInt" class="com.example.components.MessageInterceptor"/>            
            </cxf:inInterceptors>                           
        </cxf:proxy-service>
    </http:inbound-endpoint>
    <component>
        <spring-object bean="proxyService"></spring-object>         
    </component>        
    <echo-component></echo-component>   
</flow>

但我无法获取肥皂标题中的元素。我尝试使用如下方法使用拦截器

@Override
public void handleMessage(SoapMessage message) throws Fault {
    System.out.println("The headers is " + message.getHeaders() );

}

但这是在打印一个 emply Collection。

请建议我如何获得这个。

【问题讨论】:

    标签: proxy cxf mule interceptor soapheader


    【解决方案1】:

    尝试在代理服务之前在您的流程中添加自定义处理器

    <flow name="WS_In">
    <http:inbound-endpoint address="http://localhost:8080/HelloService"
        exchange-pattern="request-response">
        <custom-processor class="your.package.SoapHeaderObserver" />   
        <cxf:proxy-service wsdlLocation="classpath:helloservice.wsdl"
            namespace="http://example.org/HelloService" service="ProxyService" >
            <cxf:inInterceptors>
            <spring:bean id="inLogger"  
                class="org.apache.cxf.interceptor.LoggingInInterceptor" />  
            <spring:bean id="msgInt" class="com.example.components.MessageInterceptor"/>            
            </cxf:inInterceptors>                           
        </cxf:proxy-service>
    </http:inbound-endpoint>
    <component>
        <spring-object bean="proxyService"></spring-object>         
    </component>        
    <echo-component></echo-component>   
    

    public class SoapHeaderObserver { 
       doSomething(SoapMessage message) {
         //try to get header here
       } 
    }
    

    此外,您的自定义处理器可以实现 MessageProcessor 或 Callable。看看这里How to implement a Mule Message Observer?

    此外,该框架有很多处理器可供您使用,而不是自己构建。

    【讨论】:

    • 感谢 Jonfornari。我添加了我实施的解决方案。
    【解决方案2】:

    我的解决方案是在代理服务之前添加消息处理器。

    下面是我的处理器。

    public class SOAPHeaderExtractor implements MessageProcessor {
    
        @Override
        public MuleEvent process(MuleEvent event) {     
            try
            {
                MuleMessage inputMessage =  event.getMessage(); 
                SOAPMessage soapMsg = MessageFactory.newInstance().createMessage(null, 
                        new java.io.ByteArrayInputStream(inputMessage.getPayloadAsString().getBytes()));
                 SOAPHeader header = soapMsg.getSOAPHeader();            
                 System.out.println(header.getElementsByTagName("sampler").item(0).getTextContent() );
            }
            catch(Exception e){
                e.printStackTrace();            
            }       
            return event ;
        }
    }
    

    还有我的流程的变化。

    <http:inbound-endpoint address="http://localhost:8080/HelloService"
            exchange-pattern="request-response">
            <custom-processor class="com.example.processors.SOAPHeaderExtractor" /> 
            <cxf:proxy-service wsdlLocation="classpath:helloservice.wsdl"
                namespace="http://example.org/HelloService" service="ProxyService" >
                <cxf:inInterceptors  >
                <spring:bean id="inLogger" class="org.apache.cxf.interceptor.LoggingInInterceptor" />   
                </cxf:inInterceptors>   
            </cxf:proxy-service>        
    </http:inbound-endpoint>
    

    如果有更好的方法,请不吝赐教。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多