简体   繁体   English

如何在Java中准备SOAP请求并调用Web服务

[英]How to prepare SOAP Request and call web service in java

I am new to SOAP , I want to call web service . 我是SOAP的新手,我想调用Web服务。
I have seen a tutorial for making soap request . 我看过一个提出肥皂要求的教程。
This is the Code 这是代码

    MessageFactory mf = null;
    SOAPMessage sm = null;
    SOAPEnvelope envelope = null;
    MimeHeaders headers = null;
    SOAPBody body = null;
    SOAPElement requestData = null;
    SOAPElement requestDoc = null;
    CDATASection cdata = null;
    SOAPConnectionFactory sfc = null;
    SOAPConnection connection = null;
    URL requestUrl = null;
    SOAPMessage response = null;
    OutputStream os = null;
    String host = null;
    int port = 80;
    String path = null;
    String api_key = null;
    int socket_timeout = 0;
    String service_type = null;
    String baseXml = null;

    try {

        xmlChannelRequest = createChannelRequest(cmId, function, guId, password, estabId);

        mf = MessageFactory.newInstance();
        sm = mf.createMessage();
        envelope = sm.getSOAPPart().getEnvelope();
        envelope.addNamespaceDeclaration("soap", "http://schemas.xmlsoap.org/soap/envelope/");
        envelope.setPrefix("soapenv");

        envelope.setAttribute("xmlns:tem", "http://tempuri.org/");

        headers = sm.getMimeHeaders();
        headers.addHeader("SOAPAction", "http://tempuri.org/IPublicChannelManagerService/RequestData");

        body = envelope.getBody();
        body.setPrefix("soapenv");

        requestData = body.addChildElement("RequestData");
        requestData.setPrefix("tem");

        requestDoc = requestData.addChildElement("requestDocument", "tem", "http://tempuri.org/");


     cdata = requestDoc.getOwnerDocument().createCDATASection("<Request>\n"
                + "           <Authentication CMId=\"6438\" Function=\"31\" Guid=\"5594FB83-F4D4-431F-B3C5-EA6D7ASDSBA795\" Password=\"y656g321TR\"/>\n"
                + "           <Establishment Id=\"297867\"/>\n"
                + "        </Request>");



        requestDoc.appendChild(cdata);

        System.out.println("----------------  SOAP Request ----------------------------");

        sm.writeTo(System.out);

        sfc = SOAPConnectionFactory.newInstance();
        connection = sfc.createConnection();
        requestUrl = new URL("http://pp.xxx.yyyy.co.uk/zzzz.svc?wsdl");

        response = connection.call(sm, requestUrl);

This is working fine. 一切正常。 My question is , is there any easy way , just like passing object to method and it creates , SOAP request . 我的问题是,有什么简单的方法,就像将对象传递给方法并创建SOAP请求一样。
ex : Just like JaxB , i will send a Bean , it will generate xml. 例如: 就像JaxB一样,我将发送一个Bean,它将生成xml。

        outputStream = new ByteArrayOutputStream();

        marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(beanObject, outputStream);

You should have the WSDL file before you make the request . 发出请求之前,您应该拥有WSDL文件。 Then just generate your specific classes using eg wsimport tool and then just call the JAX-WS webservice. 然后,只需使用wsimport工具生成特定的类,然后调用JAX-WS Web服务。 You do not have to build the request by yourself in this way. 您不必以这种方式自己构建请求

Have a look over this tutorial. 看一下教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM