简单对象访问协议(SOAP):
    用于Internet上的分布式计算,可以实现跨平台跨编程语言的对象访问和方法调用.它通过HTTP协议实现参数的传输,同时以特定的XML格式表示参数.这样,只需要一个支持HTTP协议的WEB服务器和一个XML解析器就可以实现简单的SOAP功能.由于成为了标准,所以会有越来越多的软件支持它.大家若有兴趣的话可以去看SOAP1.1标准.
    同时,还有许多SOAP的开发工具,以简化SOAP的开发.随着这些开发工具的完善,我们甚至可以不用管具体的SOAP格式就可以开发SOAP应用.
下面以APACHE的SOAP包为例,举个例子:
服务器端的Class:
package samples.soap;
public class SOAPCal {
    double rate;
    public SOAPCal() {
        rate = 8.7;
    }
    public void setRate(double rate) {
        this.rate = rate;
    }
    public Double calculate(int direction, double value) {
        Double retVal;
        switch(direction) {
            case 0:
                retVal = new Double(value * rate);
                break;
            case 1:
                retVal = new Double(value / rate);
                break;
            default:
                retVal = null;
         }
        return retVal;
    }
}
客户端Class:
package samples.soap;
import java.io.*;
import java.util.*;
import java.net.*;
import org.w3c.dom.*;
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.encoding.*;
import org.apache.soap.encoding.soapenc.*;
import org.apache.soap.rpc.*;
public class SOAPCalUser {
    public static void main(String[] args) throws Exception {
        if (args.length != 3
                && (args.length != 4 || !args[0].startsWith("-"))) {
            System.err.println("Usage:");
            System.err.println("  java " + SOAPCalUser.class.getName() +
                         " [-encodingStyleURI] SOAP-router-URL name " +
                         "(0: dollor to yuan | 1: yuan to dollor) value");
            System.exit (1);
        }
        int offset = 4 - args.length;
        String encodingStyleURI = args.length == 11
                              ? args[0].substring(1)
                              : Constants.NS_URI_SOAP_ENC;
        URL url = new URL(args[1 - offset]);
        int direction = Integer.parseInt(args[2 - offset]);
        double value = Double.parseDouble(args[3 - offset]);
        Call call = new Call();
         call.setTargetObjectURI("urn:SOAPCal");
        call.setMethodName("calculate");
        call.setEncodingStyleURI(encodingStyleURI);
        
        Vector params = new Vector();
        params.add(new Parameter("direction", int.class, new Integer(direction), null));
        params.add(new Parameter("value", double.class, new Double(value), null));
        call.setParams(params);
        Response resp;
         try {
            resp = call.invoke(url, "");
        } catch (SOAPException e) {
            System.err.println("Caught SOAPException (" +
                                 e.getFaultCode() + "): " +
                                 e.getMessage());
            return;
        }
         // Check the response.
        if (!resp.generatedFault()) {
            Parameter ret = resp.getReturnValue();
            Object retVal = ret.getValue();
             System.out.println(retVal != null ? "\n" + retVal : "I don't know.");
        } else {
            Fault fault = resp.getFault();
             System.err.println("Generated fault: ");
            System.out.println ("  Fault Code   = " + fault.getFaultCode());  
            System.out.println ("  Fault String = " + fault.getFaultString());
        }
    }
}

package samples.soap;
import java.io.*;
import java.util.*;
import java.net.*;
import org.w3c.dom.*;
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.encoding.*;
import org.apache.soap.encoding.soapenc.*;
import org.apache.soap.rpc.*;
 
public class SOAPCalAdmin {
    public static void main(String[] args) throws Exception {
        if (args.length != 2
                && (args.length != 3 || !args[0].startsWith("-"))) {
            System.err.println("Usage:");
            System.err.println("  java " + SOAPCalAdmin.class.getName() 
                          + " [-encodingStyleURI] SOAP-router-URL name " +
                         "rate ");
            System.exit (1);
        }
        int offset = 3 - args.length;
        String encodingStyleURI = args.length == 11
                              ? args[0].substring(1)
                              : Constants.NS_URI_SOAP_ENC;
        URL url = new URL(args[1 - offset]);
        double rate = Double.parseDouble(args[2 - offset]);
        Call call = new Call();
        call.setTargetObjectURI("urn:SOAPCal");
        call.setMethodName("setRate");
        call.setEncodingStyleURI(encodingStyleURI);
        Vector params = new Vector();
        params.add(new Parameter("rate", double.class, new Double(rate), null));
        call.setParams(params);
        Response resp;
         try {
            resp = call.invoke(url, "");
        } catch (SOAPException e) {
            System.err.println("Caught SOAPException (" +
                                 e.getFaultCode() + "): " +
                                 e.getMessage());
            return;
        }
         // Check the response.
        if (!resp.generatedFault()) {
            System.out.println("The rate has been changed.");
        } else {
            Fault fault = resp.getFault();
             System.err.println("Generated fault: ");
            System.out.println ("  Fault Code   = " + fault.getFaultCode());  
            System.out.println ("  Fault String = " + fault.getFaultString());
        }
    }
}
 发布用的XML文件dd.xml:
<isd:service xmlns:isd="http://xml.apache.org/xml-soap/deployment"
             id="urn:SOAPCal">
  <isd:provider type="java"
                scope="Application"
                methods="setRate calculate">
    <isd:java class="samples.soap.SOAPCal"/>
  </isd:provider>
</isd:service>
这个程序的功能是设置和计算美元/人民币的兑换价格,用
java org.apache.soap.server.ServiceManagerClient http://sb3server:8080/soap/servlet/rpcrouter deploy dd.xml
把服务器端程序发布到TOMCAT上,然后在客户端用:
java samples.soap.SOAPCalAdmin http://sb3server:8080/soap/servlet/rpcrouter XX.XX
来设置汇率(1美元兑换多少人民币), 用
java samples.soap.SOAPCalUser http://sb3server:8080/soap/servlet/rpcrouter (1|0) XX.XX
来换算,其中1和0代表从人民币到美元和从美元到人民币,下一个参数是要换算的钱数.
另外,在http://www.xmethods.com/gettingstarted/apache.html
有一个简单的教程介绍怎样用APACHE的SOAP包写SOAP客户端程序.讲解比较详细.
注意点:
    *如果SOAP传回来的参数是一个CLASS,这个CALSS会被序列化(Serializer)为一段XML代码,在客户端接这个CLASS时,仍然需要一个同名CLASS,而且那些被传递过来的数据的SET和GET方法名和参数名要相同.如果是自定义的CLASS,就要做maping声明.声明方法见APACHE SOAP包的samples\addressbook\DeploymentDescriptor.xml文件中isd:mappings的写法.
在客户端调用时要加上
SOAPMappingRegistry smr = new SOAPMappingRegistry();
BeanSerializer beanSer = new BeanSerializer();
smr.mapTypes(/*encodingStyle*/Constants.NS_URI_SOAP_ENC,
      new QName(/*Namespace URI*/"urn:xml-soap-address-demo", /*Local Part*/"address"),
      /*Class type*/Address.class, /*java2XMLClassName*/beanSer, /*xml2JavaClassName*/beanSer);
call.setSOAPMappingRegistry(smr);
    *目前不是所有的CLASS都能被序列化,如Hashtable就不行,所以最好用简单的数据类型,如String,String,Integer,Double及其数组来传递变量,对复杂的参数,可以直接做成XML Document传递(使用Constants.NS_URI_LITERAL_XML作为EncodingStyleURI),这样就省得客户端专门做个CLASS来接参数,也方便跨语言的调用.在使用APACHE的SOAP包时,XML Document在客户端会被转换为XML DOM的Element class.
     现在唯一的方法是自己写进行序列化的程序,不过以后也许会有标准的序列化方法.
    *CLASS被序列化时CLASS中的静态的变量是不会被加入的来的,这一点要注意.

解决方案 »

  1.   

    apache 的soap包需要
    Apache SOAP has the following requirements:
    Java 1.1 or higher, and a servlet engine supporting version 2.1 or higher of the Java Servlet API 
    A JAXP compatible, namespace aware XML parser 
    JavaMail (mail.jar) and the JavaBeans Activation Framework (activation.jar) 
    XMI encoding requires use of Java 1.2.2 and XML4J 2.0.15. Your classpath must have xerces.jar first and then xml4j.jar next in that order. 
    Implementing services in scripting languages requires the use of Bean Scripting Framework. 
    SSL (HTTPS) support requires Java 1.2.1 or later and the Java Secure Socket Extension. 
    The SMTP transport requires the SMTP and POP3 Bean Suites. 
    Apache SOAP has the following requirements:Java 1.1 or higher, and a servlet engine supporting version 2.1 or higher of the Java Servlet API 
    A JAXP compatible, namespace aware XML parser 
    JavaMail (mail.jar) and the JavaBeans Activation Framework (activation.jar) 
    XMI encoding requires use of Java 1.2.2 and XML4J 2.0.15. Your classpath must have xerces.jar first and then xml4j.jar next in that order. 
    Implementing services in scripting languages requires the use of Bean Scripting Framework. 
    SSL (HTTPS) support requires Java 1.2.1 or later and the Java Secure Socket Extension. 
    The SMTP transport requires the SMTP and POP3 Bean Suites. 最重要的包是soap.jar
    mail.jar
    activation.jar
    jaxp.jar
    去sun的网站down吧。
      

  2.   

    再给些资源:
    http://www.xmethods.net 
    http://www.webservices.org 
    http://www.ibm.com/developerworks/webservices/ 
    http://msdn.microsoft.com/soap 
    http://www.develop.com/soap 
      

  3.   

    别人都给了你很多信息了,原来不想插一腿的,但是看你的分实在是多,我也不能不要,呵呵,我就给你一篇文章吧SOAP in the JavaTM Platform 
    August 21, 2001Guest Speakers: Rahul Sharma (Rahuls) and Nick Kassem (nickk)
    Moderator: Edward Ort (MDR-EdO)This is a moderated forum.MDR-EdO: Welcome to Java Live! Today's session in on SOAP in the JavaTM Platform -- in particular the Java API for XML-based RPC (JAX-RPC), which is used to send SOAP method calls, and Java API for XML Messaging (JAXM), which is used to send SOAP messages. Our guests are Sun engineers Rahul Sharma and Nick Kassem. Rahul is the Spec lead for JAX-RPC, and Nick is the Spec lead for JAXM. So let's begin -- who has the first question? test: What's the status of JAX-RPC and JAXM? nickk: The JAXM Specification will have its second public draft released within a few days and we hope to have an RI available within a week or so. Rahuls: JAX-RPC 1.0 Specification is presently being worked by the JSR-101 expert group under the Java Community ProcessSM. The 1.0 Specification will go for a community draft and subsequently public draft before the end of this calendar year. About JAX-RPC 1.0 RI, we are planning for a Beta release during the JavaOneSM conference (March 2002). We may have an Early Access release before that. Julio: Why two APIs? Don't they both handle the same SOAP messages? Rahuls: JAX-RPC addresses a remote procedure call based programming model, while JAXM is focused on a document-based asynchronous messaging model. These two APIs have their respective design centers and programming models. In addition, implementations of these two APIs place different requirements on the infrastructure. For example, a JAXM provider would support QoS (such as reliable message delivery) as part of the messaging infrastructure. Both APIs use SOAP as their underlying XML-based protocol. However, the JAX-RPC programming model hides SOAP based abstractions, while JAXM exposes them. JAXM expects future profiles to hide the SOAP abstraction. Clement: Would you briefly explain what SOAP is and what we can use it for? nickk: The SOAP 1.1 Specification essentially defines a web-centric message packaging model. What this means is that there is a standard way to describe a message body, header, and so on. In addition, the Specification describes bindings for transports such as HTTP. SOAP 1.2 will deal with bindings in a more formal and flexible way. A key dimension of SOAP is the ability to interoperate (on-the-wire). jhigginbotham: Are there any plans to compose a server-side SOAP container API/specification, similar to JavaTM 2, Enterprise Edition's (J2EETM)'s Enterprise JavaBeanTM (EJBTM) container Specification? If so, any plans to support an optimized (in process) communication to the SOAP container for better performance? Rahuls: JSR-109 (Java APIs for Web Services) and J2EE 1.4 would define the J2EE programming model and APIs using JAX-RPC and JAXM. In JAX-RPC 1.0, we don't plan to define the J2EE container-based programming model for SOAP based services. JSR-109 and J2EE 1.4 would address these. You should check these. shampoo: For commercial use, would it be right to use the JAX pack APIs now? nickk: The JAX pack APIs are evolving rapidly. JAXP has been available for quite awhile so you could certainly use it in a commercial environment. JAXM & JAXB are close to being ready for prototyping now. You should look out for EAs of JAX-RPC & JAXR. feili: For SOAP to work we need a map between class and XML schema. When will Sun support it? Rahuls: JAXB 1.0 defines Java data binding APIs for XML. JAXB may support XML schema as part of the roadmap of the JAXB specification. The JAXB 1.0 Specification is not yet final. As JAXB evolves and supports XML schema, JAX-RPC 1.0 would definitely consider using JAXB for type mapping and data binding. MikeW: What are the advantages of JAX* over other SOAP implementations, specifically Apache SOAP? nickk: The JAX* APIs are being developed within the JCP with the key goal of establishing standard APIs. Apache SOAP is looking to establish standard implementations. So I see the relationship as being potentially complimentary. test: What are the other messaging methods (that is, other than SOAP 1.1 with attachments) that JAXM supports? nickk: The JAXM RI will have minimal support for an ebXML MS Profile. We are also looking at other Messaging Profiles layered on SOAP, such as SOAP-RP. We haven't made any decision about specifying (in a normative manner) specific Profiles. A point worth keeping in mind is that there is really no such thing as "SOAP Messaging". So to really implement SOAP based messaging one would need to implement a Profile on top of JAXM. Corey Jackson: Do you see SOAP and these APIs replacing the need for CORBA? Rahuls: JAX-RPC is focused on an XML-based RPC model across heterogeneous platforms and environments. This fits the web services model. A Java-based JAX-RPC service client may invoke methods on a service defined and deployed on a non-Java platform. The converse is true as well. Interoperability is the key. CORBA has huge support and adoption. The J2EE 1.3 platform requires IIOP for the interoperability. In my opinion, CORBA would continue to exist and succeed as a distributed computing model. SOAP-based messaging and RPC would carve its own space for web-centric services. test: Can you contrast JAXM versus JMS with XML? What are the strengths/weaknesses of each approach? nickk: The key distinctions between JAXM and JMS+XML can be narrowed down to: Interoperability 
    Message abstraction 
    JMS offers an abstraction for Message Oriented Middleware (MOM) infrastructures. JAXM on the other hand is an abstraction for SOAP messages intended to be transported primarily over a web-based infrastructure. 
    Yuri Gadow: As SOAP, XP, and so on, continue to evolve, companies (one in particular) will introduce extensions and defects which may temporarily break compatibility with pure implementations. My question is, how will Sun be approaching these in the Java implementations, will allowances be made to facilitate near-term interoperability? Rahuls: JAX-RPC 1.0 places high importance on interoperability. SOAP-based interoperability would be the key. Our goal would be to have various JAX-RPC 1.0 implementations interoperate with .Net and other SOAP toolkits. We would stay away from any proprietary extensions that hurt interoperability. Rich K: What is a Message profile -- other than ebXML? And why is it required to really implement SOAP based messaging? nickk: A JAXM Profile represents a specific usage of SOAP enveloping, for example, ebXML uses SOAP envelopes but goes further and defines how the SOAP Header, Body and attachments should be used. To truly implement asynchronous one-way messaging one has to have the notion of a FROM:, TO: , CORRELATION_ID: and so on. SOAP 1.1 does not specify this. Profiles, however, do go beyond the SOAP enveloping scheme. Keep in mind that you don't need Profiles in simpler request-response messaging scenarios. feili: You mean for the moment we only can use primitive types for JAVA-RPC? Rahuls: JAX-RPC 1.0 would support primitive types, struct, arrays, a few extended types as defined in the XML schema specification. There would also be an extensible type mapping framework to support additional types for which standard type mapping is not defined. Rich K: You mean you can't use String? Rahuls: String.. definitely yes. Clement: Why do we need SOAP to transfer XML? Why can't we just use HTTP or other already existing protocols? What is the advantage of SOAP? nickk: You don't really have to use SOAP to exchange XML messages. However using a standard packaging model greatly facilitates interoperability. Julio2: Is there a concept of business transaction using SOAP with JAX-RPC? Rahuls: JAX-RPC does not define a business transaction model. These will be higher levels of services and programming models built on top of the base JAX-RPC model. The roadmap of the J2EE platform may address this level of services. test: What's the current status of XMLP, the W3C XML protocol? nickk: XMLP appears to be making good progress and I expect that drafts will begin to gain traction in the Jan/Feb '02 timeframe. feili: What is SUN's plan for SOAP server side connecting to J2EE's EJB, Java DataBase Connectivity (JDBCTM) and so on? Rahuls: J2EE component model for JAX-RPC and JAXM is being addressed as part of JSR-109 and J2EE 1.4 (this is not filed as a JSR yet). Your issues would be addressed as part of these JSRs. feili: Is the SOAP client side very thick? nickk: A SOAP messaging client can be very thin. JAXM supports request-response messaging for JavaTM 2, Standard Edition (J2SETM) applications. Given that request-response messaging is synchronous in nature, there is no need for the application to have an asynchronous activation framework and hence it can be quite thin. DaTa: Would Sun plan to implement SOAP on JavaTM 2, Micro Edition (J2METM) devices? Rahuls: We are exploring support for JAX-RPC for J2ME MIDP clients. For example, J2ME platform constraints (example: small footprint for client-side runtime system) are issues that come up in the space. feili: How does the Java SOAP client use .NET SOAP server's complex type? Rahuls: JAX-RPC 1.0 would support pluggable serializers and deserializers. These can be used to support complex types. samrat: Isn't JAX-RPC an RPC mechanism, which though XML-based, is different from SOAP (which also supports the messaging model)? Rahuls: JAX-RPC 1.0 supports SOAP as an XML-based protocol. But JAX-RPC APIs don't preclude any other XML-based protocol. nickk: A general plug. If you would like to influence the direction of the JAXM Specification, please review the latest public draft and provide feedback at [email protected]. Thanks. biteme: Other than HTTP and SMTP what transports will Sun be supporting? nickk: The JAXM RI team currently has no plans to support SOAP bindings other than HTTP(S) & SMTP at this time. samrat: Slightly off the track, but is there any attempt from Sun to synch between the UDDI and ebXML registries? Rahuls: The UDDI and ebXML registry are addressed under the JAXR (Java APIs for XML registry). This Specification just went public. You may access the latest information there. Yvan Frey: In Delphi there is generic code that lets you point to a wsdl file and invoke methods exposed by the wsdl. You can then use that "component" and script it or enhance it. Is there something similar for the Java platform? Rahuls: Your Delphi use case would be supported by JAX-RPC. We expect tools to layer such useful facilities on top of the JAX-RPC implementations. DaTa: Where can I find more information on SOAP and JAXM? nickk: The quickest way to track JAXM is to go to java.sun.com and search (top right hand corner) for JAXM. gfahui: How does Java support SOAP to bind, publish and resolve the WebService? nickk: This is a general question but the JAX* APIs (specifically JAXM, JAX-RPC & JAXR) are intended to support the emerging WebServices programming model namely, find-bind-run. MDR-EdO: Well, our hour has quickly come to a close. I'd like to thank all of our participants -- we had a nice set of questions. And of course I'd like to thank our guests Rahul and Nick for their excellent answers. biteme: Just a comment, a best practices guide for JAXM would be great... nickk: Agreed. Thanks for all the questions and all the best. Regards, Nick Rahuls: For more information on JAX-RPC, please check Java Technology and XML and JSRs: Java Specification Requests Detail, JSR 101 Java APIs for XML RPC. Goodbye. 
      

  4.   

    starfish很有名啊, 崇拜 ...
      

  5.   

    去www.apache.org down下soap-bin-2.2.zip和soap-src-2.2.zip。
    一个是soap包,一个是文档。有不少例子。
      

  6.   

    谢谢大家,事实上我问这个问题之前已经去www.apache.org down下载了那个包了,但是那个包需要用到很多库,而这些库又是不同的公司的,这样做出来的产品的发布和用户配置就很困难(服务器端和客户端分别至少要5个以上的包:()
    我问这个问题是想知道有没有比较集成化的解决方案,比如在VisualAge Java, 或Jbuilder中是否有这项功能。现在看来好像没有。
    不过还是谢谢大家热心地提供资料,
    我可以结分了。
    dickmi的分我另外开个帖子给:)