安装Apache SOAP
下载需要的资源:
Apache SOAP 2.3 http://apache.linuxforum.net/dist/ws/soap/version-2.3.1/soa
p-bin-2.3.1.zip
mail.jar from JavaMail http://java.sun.com/products/javamail
activation.jar from JavaBeans Activation Framework http://java.sun.com/prod
ucts/beans/glasgow/jaf.html
a JAXP compatible, namespace-aware XML parser such as Apache Xerces (v1.1.2
or later) http://xml.apache.org/xerces-j
Tomcat 4.1.x http://jakarta.apache.org/tomcat
安装tomcat:
网上很多介绍的文章,这里不再赘述。
安装Apache SOAP:
把下载回来的soap-2.3.1.zip解压到c:\soap-2_3_1。把c:\soap-2_3_1\lib下的两
个文件复制到%TOMCAT_HOME%\common\lib下面;把c:\soap-2_3_1\webapps下的soap.war
复制到%TOMCAT_HOME%\webapps下面;把其他的几个jar文件(mail.jar,activation.jar,xercesImpl.jar,xml-apis.jar)复制到%TOMCAT_HOME%\common\lib下面。
设置classpath变量:
把上面的四个jar文件路径添加到classpath变量中
%TOMCAT_HOME%\common\lib\soap.jar
%TOMCAT_HOME%\common\lib\mail.jar
%TOMCAT_HOME%\common\lib\xml-apis.jar
%TOMCAT_HOME%\common\lib\activation.jar
%TOMCAT_HOME%\common\lib\xercesImpl.jar
PS:我的做法是把这四个jar文件合成一个soap-all.jar文件,并不路径添加到clas
spath中去,这样省点事。
测试客户端配置:
1.打开web浏览器,访问http://127.0.0.1:8080/soap/servlet/rpcrouter,如果出
现提示: “Sorry, I don't speak via HTTP GET- you have to use HTTP POST to tal
k to me”,第一步测试通过。
2.命令行测试:在命令行下输入下面命令
>java org.apache.soap.server.ServiceManagerClient http://127.0.0.1:808
0/soap/servlet/rpcrouter list
如果输出只是"Deployed Services:",没有得到其他任何输出信息,否则请检查
classpath配置是否正确
运行HelloWorldService服务
HelloWorldService.java
__________________________________
public class HelloWorldService
{
String getMessage()
{
return "Hello World!";
}
}
_________________________________
创建部署描述符:
部署描述符其实就是一个xml文件。当SOAP请求被发送给org.apache.soap.server.htt
p.RPCRouterServlet时,RPCRouterServlet使用部署描述符来决定把请求路由至何处。下
面将叙述用Apache SOAP的命令行工具注册服务。
HelloWorld.xml
---------------------------------
<?xml version="1.0"?>
<isd:service xmlns:isd="http://xml.apache.org/xml-soap/deployment" id="urn
:HelloWorldService">
<isd:provider type="java" scope="Request" methods="getMessage">
<isd:java class="HelloWorldService" static="false"/>
</isd:provider>
</isd:service>
---------------------------------
注册服务:
在命令行下输入(注意要在HelloWorldService.xml文件所在目录下)
> java org.apache.soap.server.ServiceManagerClient http://127.0.0.1:8080/so
ap/servlet/rpcrouter deploy HelloWorld.xml
如果没有错误提示则注册成功,可以用下面的几个命令来查看已经注册的服务
> java org.apache.soap.server.ServiceManagerClient http://127.0.0.1:8080/so
ap/servlet/rpcrouter list
出现提示“Deployed Services: urn:HelloWorldService”
注销服务:
> java org.apache.soap.server.ServiceManagerClient http://127.0.0.1:8080/so
ap/servlet/rpcrouter undelpoy "urn:HelloWorldService"
还可以查询服务属性:
> java org.apache.soap.server.ServiceManagerClient http://127.0.0.1:8080/so
ap/servlet/rpcrouter query "urn:HelloWorldService"
HelloWorld服务客户端
HelloWorldClient.java
_______________________
import org.apache.soap.Constants;
import java.net.URL;
import org.apache.soap.Fault;
import org.apache.soap.rpc.Call;
import org.apache.soap.rpc.Response;
import org.apache.soap.rpc.Parameter;
public class HelloWorldClient {
static String DEFAULT_ENDPOINT = "http://localhost:8080/soap/servlet/rpcrout
er";
public static void main(String args[]) throws Exception {
String endPoint = DEFAULT_ENDPOINT;
//Process Arguments
if (args.length == 1)
endPoint = args[0];
else if (args.length > 1)
System.out.println("java HelloWorldClient [endpoint]");
// Build the SOAP RPC request message using the Call object
Call call = new Call();
call.setTargetObjectURI("urn:HelloWorldService");
call.setMethodName("getMessage");
call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
// Create a URL object, which represents the endpoint
URL url = new URL(endPoint);
// Send the SOAP RPC request message using invoke() method
Response resp = call.invoke(url, "");
// Check the response.
if (resp.generatedFault()) { // Error Occured
Fault fault = resp.getFault();
System.out.println("The Following Error Occured: ");
System.out.println(" Fault Code = " + fault.getFaultCode());
System.out.println(" Fault String = " + fault.getFaultString());
} else { // Completed Successfully
Parameter result = resp.getReturnValue();
System.out.println(result.getValue());
}
}
}
-----------------------------------------------
运行服务:
首先把HelloWorldService.class打包成jar文件放到%tomcat_home%/common/lib或者
直接把HelloWorldService.class放到%tomcat_home%/common/classes下
按照上面讲的方法注册服务,然后运行HelloWorldClient,屏幕输出"HelloWorld!"
--
本文地址:http://com.8s8s.com/it/it11231.htm