加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 16:22:36 所属栏目:Java 来源:网络整理
导读:我正在使用JAXWS为我们构建的 Java应用程序生成WebService客户端. 当JAXWS构建其XML以在SOAP协议中使用时,它将生成以下命名空间前缀: env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" env:Body ... !-- body goes here -- /env:Body/e
我正在使用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());

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读