2012-01-29 86 views
3

我的Java應用程序試圖從web服務獲取信息。 XML請求需要具有在XML根元素(類名)中指定的名稱空間,但標記的名稱空間(類字段)需要爲空(空),否則Web服務會拒絕該請求。Spring和CastorMarshaller:將名稱空間添加到XML根目錄

我必須使用Spring 3.0和Spring WS 2.0與CastorMarshaller(當前使用Castor版本1.3.1)將我的Java對象編組/解組到XML中。

請注意以下代碼片段中的__PREFIX____NAMESPACE__位置。

希望的編組輸出 (即所希望的生成的SOAP請求)

<?xml version="1.0" encoding="UTF-8"?> 
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap-env:Header xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" /> 
    <soap-env:Body xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"> 
     <__PREFIX__:className xmlns:__PREFIX__="__NAMESPACE__"> 
      <fieldName>fieldValue</fieldName> 
     </__PREFIX__:className> 
    </soap-env:Body> 
</soap-env:Envelope> 

目前編組輸出(即,生成的SOAP請求)

不添加命名空間

<?xml version="1.0" encoding="UTF-8"?> 
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap-env:Header xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" /> 
    <soap-env:Body xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"> 
     <className> 
      <fieldName>fieldValue</fieldName> 
     </className> 
    </soap-env:Body> 
</soap-env:Envelope> 

或添加n amespace to all elements

<?xml version="1.0" encoding="UTF-8"?> 
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap-env:Header xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" /> 
    <soap-env:Body xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"> 
     <__PREFIX__:className xmlns:__PREFIX__="__NAMESPACE__"> 
      <__PREFIX__:fieldName xmlns:__PREFIX__="__NAMESPACE__">fieldValue</__PREFIX__:fieldName> 
     </__PREFIX__:className> 
    </soap-env:Body> 
</soap-env:Envelope> 

哪些都被web服務拒絕。

我的配置

CastorMarshaller豆applicationContext.xml

<bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller"> 
    <property name="mappingLocation" value="classpath:castor-mapping.xml" /> 
    <property name="ignoreExtraAttributes" value="true" /> 
    <property name="ignoreExtraElements" value="true" /> 
    <property name="namespaceMappings"> 
     <map> 
      <entry key="__PREFIX__" value="__NAMESPACE__" /> 
     </map> 
    </property> 
</bean> 

蓖麻映射文件castor-mapping.xml

不通過添加在castorMarshaller豆指定的命名空間(命名空間應該被添加到根目錄)

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN" 
         "http://castor.org/mapping.dtd"> 
<mapping> 
    <class name="some.package.ClassName"> 
     <map-to xml="className"> 
     <field name="fieldName" type="string"> 
      <bind-xml name="fieldName" node="element" /> 
     </field> 
    </class> 
</mapping> 

或添加的命名空間來的所有元素

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN" 
         "http://castor.org/mapping.dtd"> 
<mapping> 
    <class name="some.package.ClassName"> 
     <map-to xml="className" ns-uri="__NAMESPACE__" ns-prefix="__PREFIX__"> 
     <field name="fieldName" type="string"> 
      <bind-xml name="fieldName" node="element" /> 
     </field> 
    </class> 
</mapping> 

回答

2

由於我面臨同樣的問題,即我正在考慮的解決方案如下:

  1. 創建一個攔截器擴展EndpointInterceptorAdapter
  2. 覆蓋handleResponse方法
  3. 修改以SOAP消息通過直接訪問或使用變壓器

公共類MyEndpointInterceptorAdapter延伸 EndpointInterceptorAdapter {

 @Override 
     public boolean handleResponse(MessageContext msgContext, Object endpoint) throws IOException { 

      WebServiceMessage responseMsg = msgContext.getResponse(); 
      SoapMessage soapMsg = (SoapMessage) responseMsg; 

      if(soapMsg!=null){ 
       SoapEnvelope soapEnvelope=soapMsg.getEnvelope(); 

       if(soapEnvelope!=null){ 

        SoapBody soapbody=soapEnvelope.getBody(); 

        if(soapbody!=null){ 

         Source bodySource=soapbody.getSource(); 
         if(bodySource instanceof DOMSource){ 
          DOMSource bodyDomSource=(DOMSource)bodySource; 
          Node bodyNode=bodyDomSource.getNode(); 

          if(bodyNode!=null){ 
           NodeList bodyNodeList=bodyNode.getChildNodes(); 

           if(bodyNodeList.getLength()!=0){ 
            Element root=(Element)bodyNodeList.item(0); 
            root.setAttribute("xmlns:ns", "YourURI"); 
            root.setPrefix("ns");    
           } 
          }  
         } 
        } 
       } 
      } 

      return true; 
     } 

} 
相關問題