SOAP tomcat7.0 HelloWorld

1,下载所需要的四个jar包,以及soap.war(四个jar包均在我的百度网盘中,对于jdk和tomcat的安装不多说。我的是jdk1.7 以及tomcat7.0)


2,配置环境:

1)设置TOMCAT_HOME:


2)将soap的四个jar放入tomcat/lib目录下:


3)设置classpath环境变量:


分别加入刚才的四个jar包:

C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\soap.jar;C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\mail.jar;C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\activation.jar;C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\xerces.jar;

同时将soap.war放入tomcat的webapps目录下:

启动tomcat,可以进入soap的管理目录:


3,使用eclipse进行开发:

新建工程,同时导入soap需要的所有jar:


4,开始HelloWorld


package com.test.soap.hello;


public class HelloWorldService {


	public String getMessage(){
		
		return "hello world!";
	}
}


----------------------

package com.test.soap.hello;


import java.net.URL;


import org.apache.soap.Constants;
import org.apache.soap.Fault;
import org.apache.soap.rpc.Call;
import org.apache.soap.rpc.Parameter;
import org.apache.soap.rpc.Response;


public class HelloWorldClient {


	public static void main(String[] args) throws Exception {


		String endPoint = "http://localhost:8080/soap/servlet/rpcrouter";


		Call call = new Call();// rpc call


		call.setTargetObjectURI("urn:HelloWorldService");
		call.setMethodName("getMessage");
		call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);


		URL url = new URL(endPoint);


		// start RPC request
		Response response = call.invoke(url, "");


		// deal response
		if (response.generatedFault()) {


			Fault fault = response.getFault();


			System.out.println("The Folling Error Ovvured:");
			System.out.println("Fault Code =" + fault.getFaultCode());
			System.out.println("Fault String=" + fault.getFaultString());


		} else {


			Parameter result = response.getReturnValue();
			System.out.println(result.getValue());
		}


	}


}
将HelloWorldService编译后的class文件放入Tomcat 7.0\webapps\soap\WEB-INF\classeswen目录下:



5,新建sopa服务发布配置文件,我的在c盘根目录下,文件名字为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="com.test.soap.hello.HelloWorldService" static="false"/>

    </isd:provider>

</isd:service>

6,cmd进入c盘,运行命令:

java  org.apache.soap.server.ServiceManagerClient http://127.0.0.1:8080/soap/servlet/rpcrouter  deploy  HelloWorld.xml


虽然有个小问题,但是可以直接无视。

7,进入http://localhost:8080/soap/admin/,可以看到你的soap管理界面,点击list,


继续点击urn进入,可以看到刚才已经成功发布:


8,运行HelloWorldClient.java可以看到结果。






你可能感兴趣的:(java,apache,tomcat,SOAP)