用SAAJ解决SOA集成问题

豆豆网   技术应用频道   2007年08月14日    社区交流

内容摘要:有几个基于Java的API可以用来访问服务。这些服务包括SAAJ、基于Java API for XML 的RPC(JAX-RPC)和Web服务调用框架(WSIF)。SAAJ提供了构建服务消费者以及提供者的API。

  SAAJ 1.2 API主要由javax.xml.soap包组成,它为带有多用途互连网邮件扩展协议(MIME)附件的SOAP消息提供抽象。该API提供了创建到端点的点到点连接的方法、创建并处理SOAP消息和附件的方法,以及接收和处理SOAP错误的方法。

  虽然在开发企业应用程序的时候,有几种技术供您选择,但对于不同的问题,某些技术可能更合适。选择正确的工具非常重要。

  选择SAAJ的理由是什么呢?SAAJ无疑很适合基于文档的同步或者异步Web Service。SAAJ使用简单,有助于您在Java环境中集成各种Web Service,它扩展了对文档风格的Web Service通信的自然支持(natural support)。SAAJ还支持基于标准接口上的XML消息传递,并且这一点得到了供应商的广泛支持。

  编写客户机

  清单 1显示了一个用SAAJ编写的简单的消费者,它访问了一个同步WebLogic Integration过程,来获得给定城市的温度。完整的项目可以从www.WebLogicPro.com下载。下面将讨论完成该项目所必需的步骤。

  清单 1. 用SAAJ编写的这个简单的消费者访问了一个同步WebLogic Integration过程,来获得给定城市的温度。

private static void invokeService(
     String serviceUrl, String inputXml)
{
     SOAPMessage reply = null;
     try
     {
          //Create Soap Connection
          SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
          SOAPConnection con = scf.createConnection();
          //Create Message
          MessageFactory mf = MessageFactory.newInstance();
          SOAPMessage msg = mf.createMessage();
          // Get the SOAPPart from the message
          SOAPPart soapPartInput = msg.getSOAPPart();
          // Creating an attachment Part
          AttachmentPart attachPart = msg.createAttachmentPart();
          attachPart.setContent( "This is a text attachment", "text/plain");
          // Create and set the content from the
          // input XML document
          StringReader stringReaderInput = new StringReader(inputXml);
          StreamSource ssInput = apPartInput.setContent(ssInput);
          // Save the changes to the message
          msg.saveChanges();
          // Invoke the service synchronously and
          // get the reply
          reply = con.call(msg, serviceUrl);
          // Get the SOAP body from the response
          SOAPBody outSoapBody = reply.getSOAPPart().getEnvelope().getBody();
          // Check to see if there is any SOAP Fault
          if (outSoapBody.hasFault())
          {
             // Take any actions for the exception scenario
             SOAPFault fault = outSoapBody.getFault();
             // Log the fault details
             System.out.println("SOAP Fault occurred");
             System.out.println("Fault Code is " + fault.getFaultCode());
             System.out.println("Fault String is " + fault.getFaultString());
          }
          else
          {
             // Print the output SOAP envelope
             System.out.println( reply.getSOAPPart().getEnvelope()); 
          }
          // Close the connection
          con.close();
     }
     catch (SOAPException e)
     {  
          // Print the stack trace if there are any
          // SOAPExceptions
          e.printStackTrace();
     }
}

作者:Mitsui    责编:豆豆技术应用

正在加载评论...