微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 更改使用JAXWS生成的默认XML命名空间前缀

我正在使用JAXWS为我们构建的 Java应用程序生成WebService客户端.

当JAXWS构建其XML以在SOAP协议中使用时,它将生成以下命名空间前缀:

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
   <env:Body ...>
       <!-- body goes here -->
   </env:Body>
</env:Envelope>

我的问题是我的Counterpart(一个大的汇款公司)管理我的客户端正在连接的服务器,拒绝接受WebService调用(请不要问我为什么),除非XMLNS(XML namepspace前缀是soapenv).喜欢这个:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body ...>
       <!-- body goes here -->
   </soapenv:Body>
</soapenv:Envelope>

所以我的问题是:

有没有办法我命令JAXWS(或任何其他Java WS客户端技术)使用soapenv而不是env作为XMLNS前缀生成客户端?是否有API调用来设置此信息?

谢谢!

解决方法

也许对你来说太迟了,我不知道这是否可以工作,但是你可以试试.

首先,您需要实现一个SoapHandler,并且在handleMessage方法中可以修改SOAPMessage.我不知道您是否可以直接修改该前缀,但可以尝试:

public class MySoapHandler implements SOAPHandler<SOAPMessageContext>
{

  @Override
  public boolean handleMessage(SOAPMessageContext soapMessageContext)
  {
    try
    {
      SOAPMessage message = soapMessageContext.getMessage();
      // I haven't tested this
      message.getSOAPHeader().setPrefix("soapenv");
      soapMessageContext.setMessage(message);
    }
    catch (SOAPException e)
    {
      // Handle exception
    }

    return true;
  }

  ...
}

那么你需要创建一个HandlerResolver:

public class MyHandlerResolver implements HandlerResolver
{
  @Override
  public List<Handler> getHandlerChain(PortInfo portInfo)
  {
    List<Handler> handlerChain = Lists.newArrayList();
    Handler soapHandler = new MySoapHandler();
    String bindingID = portInfo.getBindingID();

    if (bindingID.equals("http://schemas.xmlsoap.org/wsdl/soap/http"))
    {
      handlerChain.add(soapHandler);
    }
    else if (bindingID.equals("http://java.sun.com/xml/ns/jaxws/2003/05/soap/bindings/HTTP/"))
    {
      handlerChain.add(soapHandler);
    }

    return handlerChain;
  }
}

最后,您必须将您的HandlerResolver添加到您的客户端服务中:

Service service = Service.create(wsdlLoc,serviceName);
service.setHandlerResolver(new MyHandlerResolver());

原文地址:https://www.jb51.cc/java/125639.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐