1.依赖类库
环境变量:
set AXIS_HOME=c:/axis
set AXIS_LIB=%AXIS_HOME%/lib
set AXISCLASSPATH=%AXIS_LIB%/axis.jar;%AXIS_LIB%/commons-discovery.jar;
%AXIS_LIB%/commons-logging.jar;%AXIS_LIB%/jaxrpc.jar;%AXIS_LIB%/saaj.jar;
%AXIS_LIB%/log4j-1.2.8.jar;%AXIS_LIB%/xml-apis.jar;%AXIS_LIB%/xercesImpl.jar
axis的lib目录下所有的jar文件
activation-1.1.jar和mail-1.4.jar,没有这两个文件,在运行的时候会有警告信息,不过程序可以正常运行。我是在xfire中找到的这两个文件。
xmlsec-1.3.0.jar这是一个可选包,没有也不会出现警告信息。从这个可以找到它 http://xml.apache.org/security/。
2.配置服务器端
%tomcat_home% |-webapps |-axis |-WEB-INF |-web.xml |-lib |-*.jar
按照结构放置好目录和文件后,修改web.xml的内容。
<?xml version="1.0" encoding="GBK"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <listener> <listener-class>org.apache.axis.transport.http.AxisHTTPSessionListener</listener-class> </listener> <servlet> <servlet-name>AxisServlet</servlet-name> <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class> </servlet> <servlet> <servlet-name>AdminServlet</servlet-name> <servlet-class> org.apache.axis.transport.http.AdminServlet</servlet-class> <load-on-startup>101</load-on-startup> </servlet> <servlet> <servlet-name>SOAPMonitorService</servlet-name> <display-name>SOAPMonitorService</display-name> <servlet-class>org.apache.axis.monitor.SOAPMonitorService</servlet-class> <init-param> <param-name>SOAPMonitorPort</param-name> <param-value>5001</param-value> </init-param> <load-on-startup>102</load-on-startup> </servlet> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/servlet/AxisServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>*.jws</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>SOAPMonitorService</servlet-name> <url-pattern>/SOAPMonitor</url-pattern> </servlet-mapping> <!-- uncomment this if you want the admin servlet --> <!-- <servlet-mapping> <servlet-name>AdminServlet</servlet-name> <url-pattern>/servlet/AdminServlet</url-pattern> </servlet-mapping> --> <session-config> <!-- Default to 5 minute session timeouts --> <session-timeout>5</session-timeout> </session-config> <!-- currently the W3C havent settled on a media type for WSDL; http://www.w3.org/TR/2003/WD-wsdl12-20030303/#ietf-draft for now we go with the basic 'it's XML' response --> <mime-mapping> <extension>wsdl</extension> <mime-type>text/xml</mime-type> </mime-mapping> <mime-mapping> <extension>xsd</extension> <mime-type>text/xml</mime-type> </mime-mapping> <welcome-file-list id="WelcomeFileList"> <welcome-file>index.jsp</welcome-file> <welcome-file>index.html</welcome-file> <welcome-file>index.jws</welcome-file> </welcome-file-list> </web-app>
3.发布jws
HelloWorld.jws
public class HelloWorld { public String sayHello() { return "hello world!"; } }
把HelloWorld.jws复制到tomcat的webapps/axis下,然后将其改名为HelloWorld.jws。
在浏览器访问http://localhost:8080/axis/HelloWorld.jws,可以看到提示发布成功的信息。
编写客户端
package anni; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import javax.xml.namespace.QName; public class TestClient { public static void main(String [] args) { try { String endpoint = "http://localhost:8080/axis/HelloWorld.jws"; Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(new java.net.URL(endpoint)); call.setOperationName(new QName("sayHello")); String ret = (String) call.invoke(new Object[]{}); System.out.println(" got '" + ret + "'"); } catch (Exception e) { System.err.println(e.toString()); } } }
注意:使用jws只能发布没有使用package打包的类,有一定的局限性,但是比较简单,适合入门。