<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    使用SAAJ處理SOAP

    The foundation of Web services lies in the sending and receiving of messages in a standard format so that all systems can understand them. Typically, that format is SOAP. A SOAP message can be generated and sent manually, but the SOAP with Attachments API for Java (SAAJ) -- an offshoot of the Java API for XML Messaging (JAXM) -- automates many of the required steps, such as creating connections or creating and sending the actual messages. This tip chronicles the creation and sending of a synchronous SOAP message.

    The process involves five steps:

    1. Creating a SOAP connection
    2. Creating a SOAP message
    3. Populating the message
    4. Sending the message
    5. Retrieving the reply

    SAAJ is available as part of the Java Web Services Developer Pack 1.2 (see Resources). This package also includes a copy of the Tomcat Web server (so you can host your own service) and sample applications.


    The structure of a SOAP message

    I'll start by showing you the structure of the message itself. A basic SOAP message consists of an envelope with two main parts: the header and the body. The application determines how these parts are used, but the overall message must follow a specific XML structure, such as:


    Listing 1. A sample SOAP message
    				<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
                xmlns:xsd="http://www.w3.org/1999/XMLSchema">
                <SOAP-ENV:Header />
                <SOAP-ENV:Body> 
                <ns1:getPrice xmlns:ns1="urn:xmethods-BNPriceCheck"
                SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
                <isbn xsi:type="xsd:string">0672324229</isbn>
                </ns1:getPrice>
                    </SOAP-ENV:Body>
                </SOAP-ENV:Envelope> 
                

    Here, the header is empty and the body contains the payload, or the message to be delivered. In this case, it's a message requesting the price of a book.

    Notice the structure of the message. The Envelope contains the Header and Body elements, and all three are part of the http://schemas.xmlsoap.org/soap/envelope/ namespace. The application sends the message using a SOAPConnection.




    Creating the connection and the message

    The first step is to create the overall class and the connection:


    Listing 2. Create the connection
    import javax.xml.soap.SOAPConnectionFactory;
                import javax.xml.soap.SOAPConnection;
                public class SOAPTip {
                public static void main(String args[]) {
                try {
                         //First create the connection
                SOAPConnectionFactory soapConnFactory =
                SOAPConnectionFactory.newInstance();
                SOAPConnection connection =
                soapConnFactory.createConnection();
                //Close the connection
                connection.close();
                } catch(Exception e) {
                System.out.println(e.getMessage());
                }
                }
                }

    The application can send SOAP messages directly using a SOAPConnection, which is now part of the SAAJ package, or indirectly using a messaging provider, which remains in the JAXM package. In this case, the application creates the SOAPConnection object using a factory.

    A factory also creates the message itself:


    Listing 3. Creating the message object
    import javax.xml.soap.SOAPConnectionFactory;
                import javax.xml.soap.SOAPConnection;
                import javax.xml.soap.MessageFactory;
                import javax.xml.soap.SOAPMessage;
                import javax.xml.soap.SOAPPart;
                import javax.xml.soap.SOAPEnvelope;
                import javax.xml.soap.SOAPBody;
                public class SOAPTip {
                public static void main(String args[]) {
                try {
                //First create the connection
                SOAPConnectionFactory soapConnFactory =
                SOAPConnectionFactory.newInstance();
                SOAPConnection connection =
                soapConnFactory.createConnection();
                        //Next, create the actual message
                MessageFactory messageFactory = MessageFactory.newInstance();
                SOAPMessage message = messageFactory.createMessage();
                //Create objects for the message parts
                SOAPPart soapPart =     message.getSOAPPart();
                SOAPEnvelope envelope = soapPart.getEnvelope();
                SOAPBody body =         envelope.getBody();
                //Close the connection
                connection.close();
                } catch(Exception e) {
                System.out.println(e.getMessage());
                }
                }
                }

    Version differences
    If you've using another version of SAAJ, such as the Axis 1.2 beta SAAJ library, you may need to use addBodyElement instead of addChildElement.

    First, you create the message itself by using the MessageFactory. This message already contains empty versions of the basic parts, such as the envelope and header. The SOAPPart contains the envelope, and the envelope contains the body. Create references to the needed objects, such as the SOAPBody.

    Next, populate the SOAPBody:


    Listing 4. Populating the body
    ...
                import javax.xml.soap.SOAPBody;
                import javax.xml.soap.SOAPElement;
                public class SOAPTip {
                public static void main(String args[]) {
                try {
                ...
                //Create objects for the message parts
                SOAPPart soapPart =     message.getSOAPPart();
                SOAPEnvelope envelope = soapPart.getEnvelope();
                SOAPBody body =         envelope.getBody();
                        //Populate the body
                //Create the main element and namespace
                SOAPElement bodyElement =
                body.addChildElement(envelope.createName("getPrice" ,
                "ns1",
                "urn:xmethods-BNPriceCheck"));
                //Add content
                bodyElement.addChildElement("isbn").addTextNode("0672324229");
                //Save the message
                message.saveChanges();
                //Check the input
                System.out.println("\nREQUEST:\n");
                message.writeTo(System.out);
                System.out.println();
                //Close the connection
                connection.close();
                } catch(Exception e) {
                System.out.println(e.getMessage());
                }
                }
                }

    The body of the SOAP message is just like any other XML element in that you can add a child element, such as getPrice. You can then add the isbn element and its text node just as you might with a typical DOM element.

    With SAAJ you also have the opportunity to directly create the SOAPPart of the message using an external file. For example, the file prepped.msg contains the XML structure in the first listing, and can be called in lieu of building the document manually:


    Listing 5. Creating the message from an external file
    ...
                import javax.xml.soap.SOAPElement;
                import java.io.FileInputStream;
                import javax.xml.transform.stream.StreamSource;
                public class SOAPTip {
                public static void main(String args[]) {
                ...
                //Create objects for the message parts
                SOAPPart soapPart =     message.getSOAPPart();
                SOAPEnvelope envelope = soapPart.getEnvelope();
                SOAPBody body =         envelope.getBody();
                //Populate the Message
                StreamSource preppedMsgSrc = new StreamSource(
                new FileInputStream("prepped.msg"));
                soapPart.setContent(preppedMsgSrc);
                //Save the message
                message.saveChanges();
                ...
                }
                }

    The StreamSource class is typically used as part of an XSL Transformation, but here you can use it simply to obtain the FileInputStream. The result is a SOAP message that's ready to send.




    Sending the message

    With a synchronous message, sending the SOAP message and receiving a reply take place in a single step:


    Listing 6. Sending the message
    ...
                public class SOAPTip {
                public static void main(String args[]) {
                ...
                //Check the input
                System.out.println("\nREQUEST:\n");
                message.writeTo(System.out);
                System.out.println();
                        //Send the message and get a reply
                //Set the destination
                String destination =
                "http://services.xmethods.net:80/soap/servlet/rpcrouter";
                //Send the message
                SOAPMessage reply = connection.call(message, destination);
                //Close the connection
                connection.close();
                ...
                }
                }

    The actual message is sent using the call() method, which takes the message itself and a destination as arguments and returns a second SOAPMessage as a reply. In earlier versions of JAXM, the destination had to be an Endpoint object or a URLEndpoint, but now it simply has to be an object. This example uses the "Book price checker" Web service hosted by XMethods, which returns the price of the book whose ISBN is listed in the request.

    The call() method blocks until it receives the returned SOAPMessage.




    The response

    The returned SOAPMessage, reply, is a SOAP message in the same form as the message sent, and as such can be manipulated just like any other XML message. SOAP allows you to transform the reply directly using XSLT:


    Listing 7. Reading the response
    ...
                import javax.xml.transform.TransformerFactory;
                import javax.xml.transform.Transformer;
                import javax.xml.transform.Source;
                import javax.xml.transform.stream.StreamResult;
                public class SOAPTip {
                public static void main(String args[]) {
                try {
                ...
                //Send the message
                SOAPMessage reply = connection.call(message, destination);
                        //Check the output
                System.out.println("\nRESPONSE:\n");
                //Create the transformer
                TransformerFactory transformerFactory =
                TransformerFactory.newInstance();
                Transformer transformer =
                transformerFactory.newTransformer();
                //Extract the content of the reply
                Source sourceContent = reply.getSOAPPart().getContent();
                //Set the output for the transformation
                StreamResult result = new StreamResult(System.out);
                transformer.transform(sourceContent, result);
                System.out.println();
                //Close the connection
                connection.close();
                ...
                }
                }

    Create the Transformer object as you would in any XSLT application. In this case, you just want to output the content, so there's no stylesheet. Here, the content itself is the entire SOAP part of the message (as opposed to the SOAP message itself, which might include attachments). You could also extract the envelope and body before processing. The result in this case is simply System.out, but can be any choice normally available to a transformation. Transform as usual.

    posted on 2007-12-17 17:27 劉錚 閱讀(391) 評論(0)  編輯  收藏 所屬分類: WebService

    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    導航

    統(tǒng)計

    留言簿(1)

    文章分類(141)

    文章檔案(147)

    搜索

    最新評論

    主站蜘蛛池模板: 亚欧免费一级毛片| jizz免费观看| 毛片免费视频在线观看| 成人精品视频99在线观看免费| 午夜毛片不卡高清免费| 亚洲中文字幕无码中文| 成年人在线免费看视频| 日韩亚洲不卡在线视频中文字幕在线观看 | 亚洲黄色免费电影| 亚洲色图国产精品| 在线免费观看亚洲| 中中文字幕亚洲无线码| 成年美女黄网站色大免费视频| 亚洲熟女综合色一区二区三区| 午夜免费福利在线| 国产成人综合亚洲绿色| MM131亚洲国产美女久久| 三级网站免费观看| 亚洲视频小说图片| 免费a级毛片无码a∨蜜芽试看| 亚洲人成色777777精品| 亚洲第一页综合图片自拍| 大地资源网高清在线观看免费| 午夜影视日本亚洲欧洲精品一区| 在线人成精品免费视频| 亚洲国产精品无码久久| 国产亚洲精品a在线观看| 久久青草91免费观看| 亚洲欧洲日韩极速播放| 暖暖免费高清日本一区二区三区| 十八禁的黄污污免费网站| 亚洲va久久久噜噜噜久久天堂| 黄色永久免费网站| 免费的黄网站男人的天堂| 亚洲日本乱码在线观看| 国产桃色在线成免费视频| 色老头综合免费视频| 亚洲视频国产视频| 国产乱子伦片免费观看中字| 日本不卡免费新一区二区三区| 伊人久久五月丁香综合中文亚洲|