更改由 JAXWS 生成的默认 XML 命名空间前缀 [英] Changing the default XML namespace prefix generated with JAXWS

查看:60
本文介绍了更改由 JAXWS 生成的默认 XML 命名空间前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using JAXWS to generate a WebService client for a Java Application we're building.

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

When JAXWS build its XMLs to use in SOAP protocol, it generates the following namespace prefix:

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

我的问题是我的对方(一家大型汇款公司)管理我的客户所连接的服务器,拒绝接受 WebService 调用(请不要问我为什么),除非XMLNS(XML 命名空间前缀是 soapenv).像这样:

My problem is that my Counterpart (a big money transfer company) which manages the server my client is connecting to, refuses to accept the WebService call (please don't ask my why) unless the XMLNS (XML namepspace prefix is soapenv). Like this:

<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 调用来设置此信息?

Is there a way I command JAXWS (or any other Java WS client technology) to generate clients using soapenv instead of env as the XMLNS prefix? Is there an API call to set this information?

谢谢!

推荐答案

也许对你来说已经晚了,我不确定这是否可行,但你可以试试.

Maybe it's late for you and I'm not sure if this may work, but you can try.

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

First you need to implement a SoapHandler and, in the handleMessage method you can modify the SOAPMessage. I'm not sure if you can modify directly that prefix but you can try:

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 添加到您的客户端服务中:

And finally you'll have to add your HandlerResolver to your client service:

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

这篇关于更改由 JAXWS 生成的默认 XML 命名空间前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆