【问题标题】:Changing SOAPRequest Prefix in JAX-WS在 JAX-WS 中更改 SOAPRequest 前缀
【发布时间】:2013-02-19 21:22:07
【问题描述】:

如何在 JAX-WS 中更改 SOAP 请求前缀。我在句柄消息中更新了 setprofix 方法

        SOAPMessage msgs = ctx.getMessage();

        SOAPMessage sm = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage();
         sm.getSOAPPart().getEnvelope().setPrefix("soap");
         sm.getSOAPPart().getEnvelope().removeNamespaceDeclaration("env");
         sm.getSOAPHeader().setPrefix("soap");
         sm.getSOAPBody().setPrefix("soap");*/

但我仍然收到相同的请求

       <?xml version="1.0"?>
       <S:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"

我需要

      <Soap:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" 

请帮忙

【问题讨论】:

  • 你试过找到命名空间吗???尝试找到一个有点像 setNamespace() 的方法......它可能会有所帮助......还有为什么需要 Soap 而不是 s,它只是一个命名空间......
  • 您的代码 sn-p 是正确的。刚刚使用 Metro JAX-WS 发行版 2.2.1-1 进行了测试。您的下划线 JAX-WS 实现可能有问题。您使用哪个 Web 服务库?
  • 我使用 wsimport JDK 1.6 提供的 WSDL 生成了代码。构建 27。
  • 使用的是2.1.6版本
  • 这是用于入站消息还是出站消息?

标签: java web-services jax-ws soap-client


【解决方案1】:
    final SOAPMessage soapMsg = context.getMessage();
    soapMsg.getSOAPPart().getEnvelope().setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:soap", "http://schemas.xmlsoap.org/soap/envelope/");
    soapMsg.getSOAPPart().getEnvelope().removeAttributeNS("http://schemas.xmlsoap.org/soap/envelope/", "env");
    soapMsg.getSOAPPart().getEnvelope().removeAttribute("xmlns:env");
    soapMsg.getSOAPPart().getEnvelope().setPrefix("soap");
    soapMsg.getSOAPBody().setPrefix("soap");
    soapMsg.getSOAPPart().getEnvelope().getHeader().detachNode();

【讨论】:

    【解决方案2】:

    我需要将默认前缀从 S 更改为 soapenv。这是我所做的:

    1. 创建一个设置前缀的 SOAPHandler 的实现。

      public class SoapNamespaceHandler implements SOAPHandler<SOAPMessageContext>{
          private final static String SOAP_PREFIX = "soapenv";
      
          @Override
          public boolean handleMessage(final SOAPMessageContext context){
      //only update the namespace prefix for outbound messages (requests)
              final Boolean isSoapRequest = (Boolean)context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
              if (isSoapRequest){
                  try{
                      //get the soap message and envelope
                      SOAPMessage soapMsg = context.getMessage();
                      SOAPEnvelope env = soapMsg.getSOAPPart().getEnvelope();
                      //set the prefix
                      env.setPrefix(SOAP_PREFIX);        
                      // **** apply the changes to the message
                      soapMsg.saveChanges();
                  } 
                  catch (SOAPException e) {
                      e.printStackTrace();
                  }
              }
          return true;
      }
      
    2. 执行以下操作之一:

      • 创建一个用作 HandlerResolver 的 XML 文件(请参阅 Changing JAX-WS default XML namespace prefix),然后使用 @HandlerChain(file = "handler.xml") 注释您的 Web 服务客户端类

        <?xml version="1.0" encoding="UTF-8"?>
        <handler-chains xmlns="http://java.sun.com/xml/ns/javaee">
            <handler-chain>
                <handler>
                    <handler-name>mypackage.SoapNamespaceHandler</handler-name>
                    <handler-class>mypackage.SoapNamespaceHandler</handler-class>
                </handler>
            </handler-chain>
        </handler-chains>
        
      • 创建 HandlerResolver 的实现 ...

         public class SoapNamespaceHandlerResolver implements HandlerResolver {
            @SuppressWarnings({ "rawtypes" })
            @Override
            public List<Handler> getHandlerChain(PortInfo portInfo) {
                List<Handler> handlerChain = new ArrayList<Handler>();
                Handler handler = (SOAPHandler<SOAPMessageContext>) new SoapNamespaceHandler();
                String bindingID = portInfo.getBindingID();
                if (bindingID.equals("http://schemas.xmlsoap.org/wsdl/soap/http")) {
                    handlerChain.add(handler);
                } else if (bindingID.equals("http://java.sun.com/xml/ns/jaxws/2003/05/soap/bindings/HTTP/")) {
                    handlerChain.add(handler);
                }
                return handlerChain;
            }
        }
        

        ...然后通过调用以编程方式将您的 HandlerResolver 实现附加到您的 Web 服务客户端

           webServiceClient.setHandlerResolver(new SoapNamespaceHandlerResolver()); 
        

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-05
      • 1970-01-01
      • 2013-07-14
      • 2012-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多