Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring和CastorMarshaller:将名称空间添加到XML根目录_Java_Xml_Spring_Xml Namespaces_Castor - Fatal编程技术网

Java Spring和CastorMarshaller:将名称空间添加到XML根目录

Java Spring和CastorMarshaller:将名称空间添加到XML根目录,java,xml,spring,xml-namespaces,castor,Java,Xml,Spring,Xml Namespaces,Castor,我的Java应用程序试图从Web服务获取信息。XML请求需要在XML根元素(类名)中指定名称空间,但标记(类字段)的名称空间必须为空(null),否则webservice将拒绝该请求 我必须将Spring3.0和SpringWS2.0与CastorMarshaller(目前使用的是Castor版本1.3.1)结合使用,以将Java对象打包/解包到XML中 请注意以下代码段中的\uuuuuuuuuuuuuuuuuuuuuuuuuuuuu前缀和\uuuuuuuuuu命名空间位置 所需编组输出 (即所

我的Java应用程序试图从Web服务获取信息。XML请求需要在XML根元素(类名)中指定名称空间,但标记(类字段)的名称空间必须为空(null),否则webservice将拒绝该请求

我必须将Spring3.0和SpringWS2.0与CastorMarshaller(目前使用的是Castor版本1.3.1)结合使用,以将Java对象打包/解包到XML中

请注意以下代码段中的
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuu前缀
\uuuuuuuuuu命名空间
位置

所需编组输出 (即所需生成的SOAP请求)

Castor映射文件
Castor-Mapping.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>
不添加命名空间(应将通过
namespaceMappings
castorshaller
bean中指定的命名空间添加到根目录中)


或者将名称空间添加到所有元素

<?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>
<?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>

由于我面临同样的问题,我正在考虑的解决方案如下:

  • 创建一个拦截器扩展EndpointInterceptorAdapter
  • 重写HandlerResponse方法
  • 通过直接访问或使用转换器修改soap消息
  • 公共类MyEndpointInterceptorAdapter扩展 端点截取器{

          @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;
          }
    
    }
    
    <?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>
    
          @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;
          }
    
    }