为什么无法从给定的来源创建信封?

时间:2016-02-15 02:44:34

标签: java web-services soap

我试图从像这样的字符串创建soap 1.2消息:

String soapIn = "<?xml version='1.0' encoding='UTF-8'?>\n" +
            "<soapenv:Envelope xmlns:soapenv=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
            "   <soapenv:Header />\n" +
            "   <soapenv:Body>\n" +
            "      <ns:getChannelLineupInfoResponse xmlns:ns=\"http://channellineup.services.vidctlwhse.oss.cable.comcast.com/xsd\">\n" +
            "          <ns:return>\n" +
            "              <?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" +
            "              <response rows=\"1044\" type=\"success\">\n" +
            "              </response>\n" +
            "          </ns:return>\n" +
            "      </ns:getChannelLineupInfoResponse>\n" +
            "   </soapenv:Body>\n" +
            "</soapenv:Envelope>";
    MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
    logger.debug("InneoquestSoapHandler.createNewSoapResponse: about to createMessage...");
    SOAPMessage newMsg = messageFactory.createMessage(
                            new MimeHeaders(),
                            new ByteArrayInputStream(soapIn.getBytes(Charset.forName("UTF-8"))));
    logger.debug("InneoquestSoapHandler.createNewSoapResponse: about to get new envelope...");
    SOAPEnvelope newEnv = newMsg.getSOAPPart().getEnvelope();  //fails here
    logger.debug("InneoquestSoapHandler.createNewSoapResponse: about to get old envelope...");

但是得到&#34;无法从给定来源创建信封&#34;在这一行

SOAPEnvelope newEnv = newMsg.getSOAPPart().getEnvelope();  //fails here

谷歌搜索时间未能找到解决方案。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

似乎是一些图书馆问题。检查附带的罐子。以下似乎对我有用: -

import java.io.ByteArrayInputStream;
import java.nio.charset.Charset;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;

public class Test {

    public static void main(String[] args) throws Exception {
        String soapIn = "<?xml version='1.0' encoding='UTF-8'?>\n" +
                "<soapenv:Envelope xmlns:soapenv=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
                "   <soapenv:Header />\n" +
                "   <soapenv:Body>\n" +
                "      <ns:getChannelLineupInfoResponse xmlns:ns=\"http://channellineup.services.vidctlwhse.oss.cable.comcast.com/xsd\">\n" +
                "          <ns:return>\n" +
                "              <?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" +
                "              <response rows=\"1044\" type=\"success\">\n" +
                "              </response>\n" +
                "          </ns:return>\n" +
                "      </ns:getChannelLineupInfoResponse>\n" +
                "   </soapenv:Body>\n" +
                "</soapenv:Envelope>";
        MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
        System.out.println("InneoquestSoapHandler.createNewSoapResponse: about to createMessage...");
        SOAPMessage newMsg = messageFactory.createMessage(
                                new MimeHeaders(),
                                new ByteArrayInputStream(soapIn.getBytes(Charset.forName("UTF-8"))));
        System.out.println("InneoquestSoapHandler.createNewSoapResponse: about to get new envelope...");
        SOAPEnvelope newEnv = newMsg.getSOAPPart().getEnvelope();  //fails here
        System.out.println("InneoquestSoapHandler.createNewSoapResponse: about to get old envelope...");
        newMsg.writeTo(System.out);
        System.out.println("**");
        System.out.println("Envolope " + newEnv);
    }
}

输出是:

InneoquestSoapHandler.createNewSoapResponse: about to createMessage...
InneoquestSoapHandler.createNewSoapResponse: about to get new envelope...
InneoquestSoapHandler.createNewSoapResponse: about to get old envelope...
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
   <soapenv:Header/>
   <soapenv:Body>
      <ns:getChannelLineupInfoResponse xmlns:ns="http://channellineup.services.vidctlwhse.oss.cable.comcast.com/xsd">
          <ns:return>
              <![CDATA[]]></ns:return>
      </ns:getChannelLineupInfoResponse>
   </soapenv:Body>
</soapenv:Envelope>**
Envolope soapenv:Envelope

答案 1 :(得分:0)

我通过将其加载到DOMSource并使用它来创建SOAP消息来实现它:

        private SOAPMessage createNewSoapResponse(SOAPMessage msg) {
            String soapIn = "<?xml version='1.0' encoding='UTF-8' ?>\n" +
    "<soapenv:Envelope xmlns:soapenv=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
    "   <soapenv:Header />\n" +
    "   <soapenv:Body>\n" +
    "      <ns:getChannelLineupInfoResponse xmlns:ns=\"http://channellineup.services.vidctlwhse.oss.cable.comcast.com/xsd\">\n" +
    "          <ns:return>\n" +
    "              <response rows=\"1044\" type=\"success\">\n" +
    "              </response>\n" +
    "          </ns:return>\n" +
    "      </ns:getChannelLineupInfoResponse>\n" +
    "   </soapenv:Body>\n" +
    "</soapenv:Envelope>";
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true);
            SOAPMessage newMsg = null;
            try {
                DocumentBuilder builder = factory.newDocumentBuilder();
                InputSource inputSource = new InputSource(new StringReader(soapIn));
                Document doc = builder.parse(inputSource);
                DOMSource domSource = new DOMSource(doc);
                MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
ByteArrayInputStream(soapIn.getBytes(Charset.forName("UTF-8"))));
                newMsg = messageFactory.createMessage();
                SOAPPart soapPart = newMsg.getSOAPPart();
                soapPart.setContent(domSource);
                SOAPEnvelope newEnv = soapPart.getEnvelope();
                SOAPEnvelope oldEnv = msg.getSOAPPart().getEnvelope();
                addChannels(oldEnv,newEnv);
            } catch (ParserConfigurationException ex) {
                logger.debug("InneoquestSoapHandler.createNewSoapResponse: ParserConfigurationException="+ex.getMessage());      
            } catch (SAXException ex) {
                logger.debug("InneoquestSoapHandler.createNewSoapResponse: SAXException="+ex.getMessage());      
            } catch (IOException ex) {
                logger.debug("InneoquestSoapHandler.createNewSoapResponse: IOExeption="+ex.getMessage());      
            } catch (SOAPException ex) {
                logger.debug("InneoquestSoapHandler.createNewSoapResponse: SOAPException="+ex.getMessage());      
            }       
            return newMsg;
        }

虽然我必须删除此行才能使解析器工作:

        "              <?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" +

虽然如果我把它放在CDATA标签中,它可能会起作用。