代码之家  ›  专栏  ›  技术社区  ›  David Rabinowitz

强制springweb服务向响应中添加xsd命名空间

  •  4
  • David Rabinowitz  · 技术社区  · 15 年前

    我使用的是springws版本1.5.8。我的回答是这样的:

    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> 
       <SOAP-ENV:Header/> 
       <SOAP-ENV:Body> 
          ...
       </SOAP-ENV:Body> 
    </SOAP-ENV:Envelope>
    

    但是,我的客户机(与我集成的客户机)要求我添加更多的命名空间取消,以便解析成功:

    <?xml version="1.0" encoding="UTF-8"?> 
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                      xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
                      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">          
        <soapenv:Body> 
             ... 
        </soapenv:Body> 
    </soapenv:Envelope> 
    

    1 回复  |  直到 15 年前
        1
  •  4
  •   skaffman    15 年前

    您可能不需要我来告诉您,当文档中没有使用某些名称空间时,任何需要存在某些名称空间声明的SOAP客户机都会被破坏。所以我就不提了。

    但是,如果您确实希望像那样改变响应,可以使用 EndpointInterceptor ,特别是 SoapEndpointInterceptor . 你可以连接端点拦截器 as described here

    您的自定义拦截器可以实现 handleResponse messageContext.getResponse() SoapMessage 再加上你需要的东西:

    public class MyInterceptor implements SoapEndpointInterceptor {
    
        public boolean handleResponse(MessageContext messageContext, Object endpoint) throws Exception {
            SoapMessage message = (SoapMessage) messageContext.getResponse();
            message.getEnvelope().addNamespaceDeclaration(....);
            return true;
        }
    
        // ... other methods here
    }
    

    不过,添加这样的低级命名空间声明可能会有副作用,所以请小心行事。

    推荐文章