基于axis1.4的webservice实例

时间:2024-01-08 14:49:50

1.准备工作:

概念:SOAP(简单对象访问协议)、WSDL(web服务描述语言)、XML(可扩展标记语言)、axis(阿帕奇可扩展交互系统)

(1)     下载axis1.4,将axis1.4中的axis项目中WEB-INF\lib,将lib下的jar文件拷贝到工程如webservice的WEB-INF\lib下

(2)     将axis1.4中的axis项目,拷贝到tomcat的webapps目录下

(3)   配置Java和Tomcat环境,设置axis环境,类似jdk的配置方法,例如:

AXIS_HOME= E:\apache-tomcat-6.0.37-windows-x86\apache-tomcat-6.0.37\webapps\axis

AXIS_LIB=%AXIS_HOME%\lib

AXISCLASSPATH=%AXIS_LIB%\axis.jar;%AXIS_LIB%\commons-discovery-0.2.jar;%AXIS_LIB%\commons-logging-1.0.4.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     (最后两个可能没有,不是必须的)

(4)       配置web.xml添加

<?xml version="1.0" encoding="ISO-8859-1"?>

<!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>

<display-name>Apache-Axis</display-name>

<listener>

<listener-class>org.apache.axis.transport.http.AxisHTTPSessionListener</listener-class>

</listener>

<servlet>

<servlet-name>AxisServlet</servlet-name>

<display-name>Apache-Axis Servlet</display-name>

<servlet-class>

org.apache.axis.transport.http.AxisServlet

</servlet-class>

</servlet>

<servlet>

<servlet-name>AdminServlet</servlet-name>

<display-name>Axis Admin Servlet</display-name>

<servlet-class>

org.apache.axis.transport.http.AdminServlet

</servlet-class>

<load-on-startup>100</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>100</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>

<session-config>

<session-timeout>5</session-timeout>

</session-config>

<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>

2.实战

1.写一个方法类,以bean的形式作为入参

RequestBean.java

package com.webservice.request;

public class RequestBean {

private String msg;

public String getMsg()

{

return msg;

}

public void setMsg(String msg)

{

this.msg = msg;

}

}

2.写一个方法类,以bean的形式作为回参

ResultResponse.java

package com.webservice.respone;

public class ResultResponse {

private int code;

private String errorMsg;

public int getCode()

{

return code;

}

public void setCode(int code)

{

this.code = code;

}

public String getErrorMsg()

{

return errorMsg;

}

public void setErrorMsg(String errorMsg)

{

this.errorMsg = errorMsg;

}

}

3.写一个方法类service,以上面两个bean作为入参和回参

BusinessService.java

package com.webservice.service;

import com.webservice.request.RequestBean;

import com.webservice.respone.ResultResponse;

public class BusinessService {

public ResultResponse send(RequestBean requestBean)

{

ResultResponse res = new ResultResponse();

try

{

String msg = requestBean.getMsg();

System.out.println("收到消息:" + msg);

if (null != msg && !"".equals(msg))

{

res.setCode(0);

res.setErrorMsg("ok");

} else

{

res.setCode(-1);

res.setErrorMsg("msg is null");

}

} catch (Exception e)

{

res.setCode(-1);

res.setErrorMsg("exception:" + e.getMessage());

}

return res;

}

}

4.在工程的WEB-INF目录下 编写deploy.wsdd

主要用来定义service名字,描述service、入参、回参

<deployment xmlns="http://xml.apache.org/axis/wsdd/"

xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">

<service name="businessService" provider="java:RPC">

<parameter name="className" value="com.webservice.service.BusinessService" />

<parameter name="allowedMethods" value="*" />

<beanMapping qname="myNS:RequestBean" xmlns:myNS="urn:BeanService"

languageSpecificType="java:com.webservice.request.RequestBean" />

<beanMapping qname="myNS:ResultResponse" xmlns:myNS="urn:BeanService"

languageSpecificType="java:com.webservice.respone.ResultResponse" />

</service>

</deployment>

5. 在工程的WEB-INF目录下编写undeploy.wsdd

用来卸载webservice服务

<undeployment xmlns="http://xml.apache.org/axis/wsdd/">

<service name="businessService"/>

</undeployment>

6. 部署项目(启动tomcat),接下来发布接口(关键一步)

进入到tomcat的项目中的WEB-INF目录下,执行下列命令进行发布。会在此WEB-INF下生成server-config.wsdd文件(以后可以在此目录配置其他接口)

java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -lhttp://192.168.30.143:8080/webservice/servlet/AxisServlet deploy.wsdd

Ps:下面命令用于取消一个已经发布的服务

java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -lhttp://192.168.30.143:8080/webservice/servlet/AxisServlet undeploy.wsdd

 

7.获取wsdl(完成)

(1)重启tomcat,访问工程目录,如:http://192.168.30.143:8080/webservice/servlet/AxisServlet(其中webservice为工程名)

(2)可以看到新的服务方法businessService,点击一下,跳刀wsdl内容页面,ctr+A拷贝到文本编辑器,去掉开头的空格,替换去掉所有开头的“-”为空(即将- <替换为<);另存为businessService.wsdl,即可提供其他方使用,注意:如果部署在linux上,要访问linux的网页获取wsdl,不然可能会有问题

-----------------------------------------------至此发布完毕------------------------------------------------

3.测试验证

1.根据wsdl生成客户端代码

(1)首先制作生成代码工具,方便以后重用;

在D:\webservice,放个axis1.4的包,创建一个批处理文件“wsdl生成java客户端文件.bat”,内容如下:根据实际情况配置

set Axis_Lib=D:\webservice\axis1.4\axis\WEB-INF\lib

set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib%

set Output_Path=D:\webservice\Testclient

set Package=com.my.client

%Java_Cmd% org.apache.axis.wsdl.WSDL2Java -o%Output_Path% -p%Package% D:\webservice\businessService.wsdl

@echo  -------------OK-----------------

@echo ---The code has been created in the folder: Testclient---

@pause

说明:蓝色部分根据实际情况调整

Axis_Lib 是axis1.4的lib位置

Output_Path是生成客户端代码存放位置

Package 是生成客户端代码所在包

最后是wsdl文件位置

(2)生成客户端代码

将wsdl文件,如businessService.wsdl放到D:\webservice下(上一步应该做了),执行文件“wsdl生成java客户端文件.bat”,即可生成客户端代码,在Testclient目录。

2.测试接口

将生成的客户端带拷贝到java或其他web工程的src下

建立测试java类,如Test.java,测试完成

public class Test {

public static void main(String[] args) throws RemoteException,

ServiceException, MalformedURLException

{

// BusinessServiceService为接口;BusinessServiceServiceLocator为接口实现类

BusinessServiceService service = new BusinessServiceServiceLocator();

// 设置要使用的接口服务地址:多用于动态调用不同服务器上的接口

URL portAddress = new URL(

"http://192.168.30.143:8080/webservice/services/businessService");

BusinessService client = service.getbusinessService(portAddress);

// 传参数bean

RequestBean requestBean = new RequestBean();

requestBean.setMsg("wwww.servyou.com.cn");

// requestBean.setMsg("");

// 发送并接收返回的结果

ResultResponse resp = client.send(requestBean);

System.out.println("消息已发送...");

int code = resp.getCode();

String errorMsg = resp.getErrorMsg();

System.out.println("消息已返回,code=" + code + ",errorMsg=" + errorMsg);

}

}