首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

使用SAAJ发送和接收SOAP消息(2)

使用SAAJ发送和接收SOAP消息(2)

接着,填充 SOAPBody:
清单4. 填充主体
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
...
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());
        }
    }
}




SOAP 消息的主体就好像是另一个 XML 元素,可以在其中添加孩子元素,比如 getPrice。然后就可以像处理典型的 DOM 元素一样,为它添加 isbn 元素和文本节点。
使用 SAAJ 还有可能使用外部文件来直接创建消息的 SOAPPart。例如,第一个程序清单中,prepped.msg 文件包含了 XML 结构,在手工建立文档的地方可以调用该文件:
清单5. 从一个外部文件创建消息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
...
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();
...
    }
}




通常,StreamSource 类用作 XSL Transformation 的一部分,但在这儿可以简单地使用它来获得 FileInputStream。结果是一个 SOAP 消息已经就绪,可以发送了。
发送消息对于同步消息,发送 SOAP 消息,并接收在同一步中发生的响应:
清单6. 发送消息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
...
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();         
...
    }
}




实际的消息是使用 call()方法发送的,该方法接收消息本身和目的地作为参数,并返回第二个 SOAPMessage 作为响应。在早期版本的 JAXM 中,目的地必须是一个 Endpoint 对象或一个 URLEndpoint,但现在它只要是一个对象就可以了。这个例子使用了由 XMethods 支持的“书价检验程序” Web 服务,该服务返回请求中给定 ISBN 的书的价格。
call()方法将会阻塞,直到它接收到返回的 SOAPMessage 为止。
响应相应地,返回的 SOAPMessage 是一个与发送消息具有相同形式的 SOAP 消息,这样该消息操作时就可以像另一个 XML 消息一样。SOAP 允许直接使用 XSLT 来转换响应:
清单7.读取响应
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
...
        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();
...            
     }
}




像在任意 XSLT 应用程序中一样,创建 Transformer 对象。在本例中,只需要输出内容,因此没有样式表。这里,内容本身就是消息的整个 SOAP 部分(与 SOAP 消息本身相反,它可能包含附件)。您也可以在处理前提取信封和主体。在本例中,结果就是 System.out,但一般情况下,可以是一次转换可用的任意选择。像平常一样地进行转换。
图1. SOAP 请求和响应接下来的步骤这个简单的应用程序仅仅输出所接收到的消息,但是您可以同样简单地从 XML 文档中提取信息。同样,虽然这一技巧仅演示了消息的同步发送和接收,但是 JAXMAPI 也允许用于同步传送的消息提供者的使用,只是通过使用 ProviderConnection 对象而不是 SOAPConnection 进行。提供者保持消息,直到该消息成功传送为止。
返回列表