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

查看:641
本文介绍了更改使用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>

我的问题是我的对手(一家大转会公司)管理我的客户连接的服务器to,拒绝接受WebService调用(请不要问我的原因),除非XMLNS(XML namepspace前缀是 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天全站免登陆