【问题标题】:Soap client : SAAJ0514 unable to create envelope from given source because the root element is not named EnvelopeSoap 客户端:SAAJ0514 无法从给定源创建信封,因为根元素未命名为信封
【发布时间】:2018-02-14 17:00:48
【问题描述】:

我正在尝试实现一个 SOAP 客户端(我找到了代码 here 并试图使其适应我的问题(将文件发送到 endPointURL))。 我只有 soapAction 和 endPointURL 将我的消息发送到服务器。

public class SOAPClient {

private static final Logger logger = LoggerFactory.getLogger(SOAPClient.class);
private static File file = null;

public static void main(String args[]) {

    String soapEndpointUrl = "url?wsdl";
    String soapAction = "soapAction(https)"
    file = new File("myFileToSend.xml");
    callSoapWebService(soapEndpointUrl, soapAction,file);
}

private static void createSoapEnvelope(SOAPMessage soapMessage,Document doc) throws SOAPException {


    SOAPPart soapPart = soapMessage.getSOAPPart();

    DOMSource domSource = new DOMSource(doc);
    soapPart.setContent(domSource);

    //Error here !
    SOAPEnvelope envelope = soapMessage.getSOAPPart().getEnvelope();

    SOAPHeader header = envelope.getHeader();



}

private static void callSoapWebService(String soapEndpointUrl, String soapAction,File f) {
    try {
        file = f;
        // Create SOAP Connection
        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection soapConnection = soapConnectionFactory.createConnection();

        // Send SOAP Message to SOAP Server
        SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(soapAction), soapEndpointUrl);

        if(soapResponse!=null){
            // Print the SOAP Response
            logger.info("Response SOAP Message:");
            soapResponse.writeTo(System.out);
            logger.info("----------------------------------------------");

            logger.info("soap reponse : "+soapResponse);

        }
        soapConnection.close();
    } catch (Exception e) {
        logger.error("\nError occurred while sending SOAP Request to Server!\n");
        e.printStackTrace();
    }
}

private static SOAPMessage createSOAPRequest(String soapAction) throws Exception {
    Document doc = addXmlFile();
    SOAPMessage soapMessage = null;

    if(doc!=null){

        MessageFactory messageFactory = MessageFactory.newInstance();
        soapMessage = messageFactory.createMessage();
        createSoapEnvelope(soapMessage,doc);
        soapMessage.saveChanges();

        logger.info(soapMessage.toString());

        /* Print the request message, just for debugging purposes */
        logger.info("Request SOAP Message:");
        soapMessage.writeTo(System.out);
    }

    return soapMessage;
}


@SuppressWarnings("finally")
private static Document addXmlFile() {
    Document doc = null;
    try {

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        doc = builder.parse(file.getAbsoluteFile());
        return doc;

    } catch (SAXException | IOException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }

    finally {

        return doc;

    }
}

实际上,当我尝试启动此代码时,我收到错误:SAAJ0514 无法从给定源创建信封,因为根元素未命名为信封。 还有错误 SAAJ0511 : Impossible to create an envelope from the given source

com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Unable to create envelope from given source: 
    at com.sun.xml.internal.messaging.saaj.soap.EnvelopeFactory.createEnvelope(EnvelopeFactory.java:117)
    at com.sun.xml.internal.messaging.saaj.soap.ver1_1.SOAPPart1_1Impl.createEnvelopeFromSource(SOAPPart1_1Impl.java:69)
    at com.sun.xml.internal.messaging.saaj.soap.SOAPPartImpl.getEnvelope(SOAPPartImpl.java:125)
    at SOAPClient.createSoapEnvelope(SOAPClient.java:70)
    at SOAPClient.createSOAPRequest(SOAPClient.java:132)
    at SOAPClient.callSoapWebService(SOAPClient.java:103)
    at SOAPClient.main(SOAPClient.java:39)
Caused by: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Unable to create envelope from given source because the root element is not named "Envelope"
    at com.sun.xml.internal.messaging.saaj.soap.SOAPPartImpl.lookForEnvelope(SOAPPartImpl.java:154)
    at com.sun.xml.internal.messaging.saaj.soap.SOAPPartImpl.getEnvelope(SOAPPartImpl.java:121)
    at com.sun.xml.internal.messaging.saaj.soap.EnvelopeFactory.createEnvelope(EnvelopeFactory.java:110)
    ... 6 more

我在许多网站上看到了一些类似的错误,但没有一个能真正帮助我找到解决方法(一些解释删除服务器上的一些参数)。

感谢您提供可能的答案。

【问题讨论】:

  • 您是否尝试使用 SOAP UI 向端点发送示例 SOAP 消息?我建议你先试试。
  • 我会在 SOAP UI 上试试!感谢您的建议
  • 嘿,我使用了 SOAP UI,它可以工作。但我总是有错误。我想我没有正确的架构: schemas.xmlsoap.org/soap/envelope"> 在许多其他代码上我发现了这个 uri: w3.org/2003/05/soap-envelope" > 我没有成功删除这个 uri 并放入新的。我试试这个:envelope.addNamespaceDeclaration("soapenv","http://www.w3.org/2003/05/soap-envelope"); envelope.removeNamespaceDeclaration(envelope.getPrefix()); 但它不起作用。你知道如何处理吗?感谢您提供可能的答案。
  • 我发现错误:String soapEndpointUrl = "url?wsdl";必须是String soapEndpointUrl = "url";

标签: java web-services soap soap-client


【解决方案1】:

我在与我合作的公司中遇到了这个错误,删除 ?wsdl 部分正是我需要做的事情才能让它工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    相关资源
    最近更新 更多