更改JAX-WS默认XML名称空间前缀 [英] Changing JAX-WS default XML namespace prefix

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

问题描述

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

I've generated source codes for an old Web Service using JAX-WS 2.1.7. When I call this service the generated soap message is something like this:

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

但旧的网络服务只接受以下格式:

But the old web service only accepts this format:

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

正如您所看到的前缀是soap而不是env并且没有标题所以我得到了抱怨肥皂:身体的错误是必需的。我无法更改旧的Web服务,需要发送兼容的soap消息。如何将前缀更改为soap并删除Header?

As you see the prefix is "soap" instead "env" and there is no header so i got an error complaining about "soap:Body" is required. I can't change the old web service and need to send compatible soap messages. how can i change the prefix to "soap" and also remove "Header"?

推荐答案

您需要创建一个实现 SOAPHandler< SOAPMessageContext> 的类这包括以下内容:

You need to create a class that implements SOAPHandler<SOAPMessageContext> and that includes something like this:

  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文件:

Then create a handler.xml file:

<?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>

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

And add an annotation to your web service:

@HandlerChain(file =handler.xml)

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

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