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

前端之家收集整理的这篇文章主要介绍了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调用来设置此信息?

谢谢!

解决方法

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

首先,您需要实现一个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.f2er.com/java/125639.html

猜你在找的Java相关文章