【问题标题】:Changing JAX-WS default XML namespace prefix更改 JAX-WS 默认 XML 命名空间前缀
【发布时间】:2012-05-21 01:50:02
【问题描述】:

我使用 JAX-WS 2.1.7 为旧的 Web 服务生成了源代码。当我调用此服务时,生成的肥皂消息是这样的:

<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'>
  <env:Header>
  </env:Header>
  <env:Body>
      ...
  </env:Body>
</env:Envelope>

但旧的网络服务只接受这种格式:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    ...
  </soap:Body>
</soap:Envelope>

正如您所见,前缀是“soap”而不是“env”,并且没有标题,所以我收到一个错误,抱怨“soap:Body”是必需的。我无法更改旧的网络服务,需要发送兼容的肥皂消息。如何将前缀更改为“soap”并删除“Header”?

【问题讨论】:

    标签: java jax-ws


    【解决方案1】:

    您需要创建一个实现SOAPHandler&lt;SOAPMessageContext&gt; 的类,其中包括以下内容:

      public boolean handleMessage(final SOAPMessageContext context)
      {
        final Boolean isSoapResponse = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
        if (!isSoapResponse)
        {
          try
          {
            final SOAPMessage soapMsg = context.getMessage();
            soapMsg.getSOAPPart().getEnvelope().setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:soap", "http://schemas.xmlsoap.org/soap/envelope/");
            soapMsg.getSOAPPart().getEnvelope().removeAttributeNS("http://schemas.xmlsoap.org/soap/envelope/", "env");
            soapMsg.getSOAPPart().getEnvelope().removeAttribute("xmlns:env");
            soapMsg.getSOAPPart().getEnvelope().setPrefix("soap");
            soapMsg.getSOAPBody().setPrefix("soap");
            soapMsg.getSOAPPart().getEnvelope().getHeader().detachNode();
          }
          catch (SOAPException e)
          {
            e.printStackTrace();
          }
        }
        return true;
      }
    

    然后创建一个handler.xml文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <handler-chains xmlns="http://java.sun.com/xml/ns/javaee">
      <handler-chain>
        <handler>
          <handler-name>test.MySoapHandler</handler-name>
          <handler-class>test.MySoapHandler</handler-class>
        </handler>
      </handler-chain>
    </handler-chains>
    

    并为您的网络服务添加注释:

    @HandlerChain(file = "handler.xml")

    【讨论】:

    • 它对我有用,稍作改动,我需要删除soapMsg.getSOAPPart().getEnvelope().getHeader().detachNode();
    • 亲爱的,我在这里遇到了同样的问题,我需要更改输出请求的前缀,我正在用一个休息请求包装一个肥皂请求,在休息 api 中我调用了肥皂,但我需要在发送请求之前更改前缀我不太确定如何在我的情况下应用建议的解决方案..任何帮助..提前谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多