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

2024-03-11

我正在使用 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>

我的问题是我的对应方(一家大型汇款公司)管理我的客户端连接到的服务器,拒绝接受 WebService 调用(请不要问我为什么)除非 XMLNS(XML 命名空间前缀是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 as the XMLNS字首?有没有一个API call设置这个信息?

Thanks!


也许对你来说已经晚了,我不确定这是否有效,但你可以尝试。

首先,您需要实现一个 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());
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

更改使用 JAXWS 生成的默认 XML 命名空间前缀 的相关文章

随机推荐