SOAP前缀soapenv已绑定到http://

时间:2014-10-01 19:33:12

标签: java soap jax-ws soap-client

我试图将自定义命名空间声明添加到SOAP请求中,更具体地说:

env.addNamespaceDeclaration("soapenv", "http://www.w3.org/2003/05/soap-envelope");

我在SOAP Message Handler中执行此操作,并且根据我使用的Web服务的规范,绝对需要此命名空间声明。但是看起来,jax-ws库总是在发送消息之前尝试添加这个soapenv命名空间声明,然后像这样失败:

Exception in thread "main" javax.xml.ws.WebServiceException: javax.xml.stream.XMLStreamException: prefix soapenv has been already bound to http://www.w3.org/2003/05/soap-envelope. Rebinding it to http://schemas.xmlsoap.org/soap/envelope/ is an error
    at com.sun.xml.internal.ws.encoding.StreamSOAPCodec.encode(StreamSOAPCodec.java:101)
    at com.sun.xml.internal.ws.encoding.SOAPBindingCodec.encode(SOAPBindingCodec.java:267)
    at com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.process(HttpTransportPipe.java:196)
    at com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.processRequest(HttpTransportPipe.java:122)
    at com.sun.xml.internal.ws.transport.DeferredTransportPipe.processRequest(DeferredTransportPipe.java:95)
    at com.sun.xml.internal.ws.api.pipe.Fiber.__doRun(Fiber.java:626)
    at com.sun.xml.internal.ws.api.pipe.Fiber._doRun(Fiber.java:585)
    at com.sun.xml.internal.ws.api.pipe.Fiber.doRun(Fiber.java:570)
    at com.sun.xml.internal.ws.api.pipe.Fiber.runSync(Fiber.java:467)
    at com.sun.xml.internal.ws.client.Stub.process(Stub.java:308)
    at com.sun.xml.internal.ws.client.sei.SEIStub.doProcess(SEIStub.java:163)
    at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:98)
    at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:78)
    at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(SEIStub.java:135)
    at com.sun.proxy.$Proxy32.getAccountBalance(Unknown Source)
    at XXXX.ClientTest.getAccountData(ClientTest.java:60)
    at XXXX.ClientTest.<init>(ClientTest.java:55)
    at XXXX.ClientTest.main(ClientTest.java:30)
Caused by: javax.xml.stream.XMLStreamException: prefix soapenv has been already bound to http://www.w3.org/2003/05/soIIap-envelope. Rebinding it to http://schemas.xmlsoap.org/soap/envelope/ is an error
    at com.sun.xml.internal.stream.writers.XMLStreamWriterImpl.writeNamespace(XMLStreamWriterImpl.java:1035)
    at com.sun.xml.internal.ws.util.DOMUtil.writeTagWithAttributes(DOMUtil.java:173)
    at com.sun.xml.internal.ws.message.saaj.SAAJMessage.writeTo(SAAJMessage.java:341)
    at com.sun.xml.internal.ws.encoding.StreamSOAPCodec.encode(StreamSOAPCodec.java:98)
    ... 17 more

handleMessage函数:

public boolean handleMessage(SOAPMessageContext context) {

    //Inquire incoming or outgoing message.
    boolean outbound = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

    try {

        if (outbound) {
            // OUTBOUND

            System.out.println("Direction=outbound (handleMessage)");
            SOAPMessage msg = ((SOAPMessageContext) context).getMessage();

            // get SOAP-Part
            SOAPPart sp = msg.getSOAPPart();

            //edit Envelope
            SOAPEnvelope env = sp.getEnvelope();
            env.addNamespaceDeclaration("web", "http://example.com/webservices");
            env.addNamespaceDeclaration("soapenv", "http://www.w3.org/2003/05/soap-envelope");
            env.removeNamespaceDeclaration("S");

            env.setPrefix("soapenv");


            SOAPHeader soapHeader = env.addHeader();
            SOAPBody soapBody = env.getBody();

            soapHeader.setPrefix("soapenv");
            soapBody.setPrefix("soapenv");

            Name ticketHeaderName = env.createName("TicketHeader", "web",
                   "http://example.com/webservices");
            SOAPElement ticketHeader =  soapHeader.addChildElement(ticketHeaderName);

            SOAPElement soapHeaderElem2 = ticketHeader.addChildElement("SessionId", "web");
            soapHeaderElem2.addTextNode(sessionId);

            msg.saveChanges();

            // print SOAP-Message
            dumpSOAPMessage(msg);

        } else {
            // INBOUND

            System.out.println("Direction=inbound (handleMessage)");
            SOAPMessage msg = ((SOAPMessageContext) context).getMessage();
            dumpSOAPMessage(msg);
         }

    } catch (Exception e) {

        //All other unhandled problems.
        e.printStackTrace();
    }
    return true;
}

1 个答案:

答案 0 :(得分:0)

使用SOAP 1.1规范

时会发生此错误
 <soapenv:Envelope xmlns:SOAPENV="http://schemas.xmlsoap.org/soap/envelope/"  xmlns:web="http://saxobank.com/webservices/">

但是您尝试将名称空间设置为SOAP 1.2版本:

 <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"  xmlns:web="http://saxobank.com/webservices/">

写出正确规范的请求为我解决了问题。