当前位置: 代码迷 >> Web前端 >> 运用axis搭建webservice
  详细解决方案

运用axis搭建webservice

热度:136   发布时间:2012-08-24 10:00:21.0
使用axis搭建webservice

1.首先创建个WEB工程,然后: http://ws.Apache.org/axis/网站下载Axis安装包.当然还依赖其他包的.

2.搭建环境,把sxis解压后webapps下的axis放到tomcat里,并把依赖的其它包放入axisd的lib里。完整包见附件。

3.配置环境变量:
AXIS_HOME=%CATALINA_HOME%\webapps\axis
AXIS_LIB=%AXIS_HOME%\lib
AXID_CLASSPATH=%AXIS_LIB%\activation.jar;%AXIS_LIB%\axis.jar;%AXIS_LIB%\axis-ant.jar;%AXIS_LIB%\commons-discovery-0.2.jar;%AXIS_LIB%\commons-logging-1.0.4.jar;%AXIS_LIB%\jaxrpc.jar;%AXIS_LIB%\log4j-1.2.8.jar;%AXIS_LIB%\mailapi_1_3_1.jar;%AXIS_LIB%\saaj.jar;%AXIS_LIB%\wsdl4j-1.5.1.jar;%AXIS_LIB%\xml-apis-2.6.2.jar;%AXIS_LIB%\xmlsec-1.4.2.jar
CLASSPATH=.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;%AXISCLASSPATH%;

4.然后就写wsdd 文件, 为了生成这个文件:server-config.wsdd。
deploy.wsdd
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
  <service name="myWebProject" provider="java:RPC">
    <parameter name="className" value="com.Sample"/>
    <parameter name="allowedMethods" value="sayHello"/>
  </service>
</deployment>


5.在DOS下转换目录到%TOMCAT_HOME%\webapps\axis\WEB-INF,前提是把上面写的wsdd文件放在此目录下。然后命令:
java -Djava.ext.dirs=C:\apache-tomcat-6.0.26\webapps\axis\WEB-INF\lib org.apache.axis.client.AdminClient deploy.wsdd

6.经上步在WEB-INF下生成server-config.wsdd文件。此文件也可以自己手动编写,如下:
<?xml version="1.0" encoding="UTF-8"?>
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
 <globalConfiguration>
  <parameter name="sendMultiRefs" value="true"/>
  <parameter name="disablePrettyXML" value="true"/>
  <parameter name="adminPassword" value="admin"/>
  <parameter name="attachments.Directory" value="C:\apache-tomcat-6.0.26\webapps\axis\WEB-INF\attachments"/>
  <parameter name="dotNetSoapEncFix" value="true"/>
  <parameter name="enableNamespacePrefixOptimization" value="false"/>
  <parameter name="sendXMLDeclaration" value="true"/>
  <parameter name="sendXsiTypes" value="true"/>
  <parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl"/>
  <requestFlow>
   <handler type="java:org.apache.axis.handlers.JWSHandler">
    <parameter name="scope" value="session"/>
   </handler>
   <handler type="java:org.apache.axis.handlers.JWSHandler">
    <parameter name="scope" value="request"/>
    <parameter name="extension" value=".jwr"/>
   </handler>
  </requestFlow>
 </globalConfiguration>
 <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/>
 <handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder"/>
 <handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/>
 <service name="myWebProject" provider="java:RPC">
  <parameter name="allowedMethods" value="sayHello"/>
  <parameter name="className" value="com.cc008.Sample"/>
 </service>
 <transport name="http">
  <requestFlow>
   <handler type="URLMapper"/>
   <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>
  </requestFlow>
 </transport>
</deployment>


5.更改项目web.xml,加入如下代码(只针对项目)
<servlet>
        <servlet-name>AxisServlet</servlet-name>
        <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>AxisServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>


6.编写服务器端代码
Sample.java
public class Sample {
	public String sayHello(String name) {  
		return "hello====>" + name;  
	}  
}


7.编写客户端代码
package client;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

public class Client {

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

		// 指出service所在URL     

		String endpoint = "http://localhost:8080/axis/services/myWebProject";

		// 创建一个服务(service)调用(call)     

		Service service = new Service();

		Call call = (Call) service.createCall();// 通过service创建call对象     

		// 设置service所在URL     

		call.setTargetEndpointAddress(new java.net.URL(endpoint));

		// 方法名(processService)与MyService.java方法名保持一致     

		call.setOperationName("sayHello");

		// Object 数组封装了参数,参数为"This is Test!",调用processService(String arg)     

		String ret = (String) call.invoke(new Object[] { "继中" });

		System.out.println(ret);

	}

}


8.启动tomcat服务器,运行http://localhost:8080/axis/services,有显示,则代表服务端提供的接口可以成功访问了。

9.运行客户端,看有没有回应。
hello====>继中

  相关解决方案