Axis开发webservice的简单实例

blog迁移至:[url=http://www.micmiu.com]http://www.micmiu.com[/url]

本文主要记录Axis开发webservice简单实例的详细过程和步骤:
Axis官方网站:[url=http://ws.apache.org/axis/]http://ws.apache.org/axis/[/url]
可以在官网下载最新1.4的包:[color=red]axis-bin-1_4.zip[/color]
将解压后的[color=red]axis-1_4\webapps\[/color]下的axis目录考到%TOMCAT_HOME%/Webapps/目录下
启动tomcat后在浏览器里输入[color=blue]http://localhost:8082/axis[/color]会看到下图所示(ps:本人的tomcat端口为8082)
[img]http://dl.iteye.com/upload/attachment/259502/291ffbca-c6eb-3356-8ed9-f4ec0b3216cd.jpg[/img]
点击上图中的[color=blue]Validataion[/color]链接,页面上会提示已经有的包和缺少的包的信息,根据提示将必须的包下载全,将这些类包复制到 [color=red]%tomcathome%/webapps/axis/WEB-INF/lib/[/color]目录下重新启动tomcat,直到Validation页面中看不到有Error与Warning的提示信息。
如果提示缺少[url=http://dl.iteye.com/topics/download/89f8b6a6-9a23-340d-a445-11ee99d421d7]xmlsec.jar[/url]可以到[url=http://santuario.apache.org/dist/java-library/]http://santuario.apache.org/dist/java-library/[/url]下载.
Axis支持三种web service的客户端访问方式,分别为:[list]
[*]Dynamic Invocation Interface ( DII)
[*]Dynamic Proxy方式
[*]Stubs方式
[/list][color=red]PS:[/color]看到很多资料将上述方式列为Web Servcie的三种“部署和开发方法,个人觉得有些欠妥
下面介绍axis部署和发布web service的方式:
[color=blue]一、JWS[/color]
JWS(Java Web Service)是最简单的一种方式。Axis允许把普通Java类的源文件的扩展名改为.jws,然后把它简单的copy到AXIS_HOME下。这样,Axis 会自动编译.jws文件,并把它自动加入到Java Web Servie的服务中。具体过程如下
1.用Eclipse或者文本编辑器编写一个java类SayHello.java([color=red]此类不含包名[/color])
public class SayHello{
public String sayMsg(String name){
return "Hello,"+name;
}
}

2.将上面的类(SayHello.java)copy到%tomcat_home%/webapps/axis/目录下,只需要把类的源文件([color=red]不是class[/color])到这个目录下,重命名为:[color=red]SayHello.jws[/color]
3.打开浏览器输入:http://localhost:8082/axis/SayHello.jws 会看到
[img]http://dl.iteye.com/upload/attachment/259404/d287154d-a1b7-3185-9612-08bd7bb747a1.jpg[/img]
点击上图Click to see the WSDL 的链接,就可以看到生成的wsdl。
4.客户端[color=blue]Dynamic Invocation Interface ( DII)方式[/color] 实现如下:
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

/**
* axis client
* @author Michael sun
*/
public class TestClient {

/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String wsdlUrl = "http://localhost:8082/axis/SayHello.jws";
// String wsdlUrl=”http://localhost:8080/axis/SayHello.jws?wsdl”
Service s = new Service();
Call call = (Call) s.createCall();
call.setOperationName("sayMsg");// 这里是要调用的方法名
call.setTargetEndpointAddress(wsdlUrl);// 设置调用的wsdl
String val = (String) call.invoke(new Object[] { "My Michael Sun" });
System.out.println("这是webservice服务器返回的信息:" + val);
}
}


5.客户端[color=blue]Dynamic Proxy方式[/color] 实现如下:
public interface SayHelloInterface extends java.rmi.Remote {

public String sayMsg(String name) throws java.rmi.RemoteException;

}

import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;

/**
* test Dynamic Proxy client
* @author Michael sun
*/
public class TestProxyClient {

/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String wsdlname = "http://localhost:8088/axis/SayHello.jws?wsdl";
// 服务路径
String namespaceUrl = "http://localhost:8088/axis/SayHello.jws";
// 服务名
String serviceName = "SayHelloService";
// 服务
String portName = "SayHello";
// 创建代理对像
ServiceFactory service = ServiceFactory.newInstance();
// 创建远程服务
Service s = service.createService(new URL(wsdlname), new QName(
namespaceUrl, serviceName));

SayHelloInterface proxy = (SayHelloInterface) s.getPort(new QName(
namespaceUrl, portName), SayHelloInterface.class);
System.out.println(proxy.sayMsg("Blue boy!"));
}
}


[color=blue]二、WSDD(Web Service Deployment Descriptor)文件发布Web Service[/color]
1.自定义参数bean和server的代码如下:
package wsaxis.bean;

/**
* bean
* @author Michael sun
*/
public class UserBean {

private String userName;

private Integer age;

/**
* @return the userName
*/
public String getUserName() {
return userName;
}

/**
* @return the age
*/
public Integer getAge() {
return age;
}

/**
* @param pUserName the userName to set
*/
public void setUserName(String pUserName) {
userName = pUserName;
}

/**
* @param pAge the age to set
*/
public void setAge(Integer pAge) {
age = pAge;
}

}

package wsaxis;

import wsaxis.bean.UserBean;

/**
* axis server
* @author Michael sun
*/
public class MessageService {

/**
* getBeanStr
* @param bean
* @return String
*/
public String getBeanStr(UserBean bean) {
return "You Name:" + bean.getUserName() + " , You Age:" + bean.getAge();
}

/**
* checkUser
* @param bean
* @return String
*/
public String checkUser(UserBean bean) {
if ("Michael".equals(bean.getUserName())) {
return "Michael welcome to axis ";
} else {
return bean.getUserName() + " can't access this ws";
}

}

}

2.deploy.wsdd和undeploy.wsdd文件的编写如下:
WSDD文件描述可参见:[url=http://www.oio.de/axis-wsdd/]http://www.oio.de/axis-wsdd/[/url]
<deployment name="test" xmlns="http://xml.apache.org/axis/wsdd/" 
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<service name="MessageService" provider="java:RPC" style="rpc" use="encoded">
<parameter name="className" value="wsaxis.MessageService"/>
<parameter name="allowedMethods" value="*"/>
<typeMapping xmlns:ns1="http://wsaxis.michael.com" qname="ns1:userBean"
type="java:wsaxis.bean.UserBean"
serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
/>
</service>
</deployment>

<undeployment
xmlns="http://xml.apache.org/axis/wsdd/">
<!-- Services from MessageService WSDL service -->
<service name="MessageService"/>
</undeployment>

3.将上面写好的两个类的class文件复制到[color=red]%tomcat_home%/axis/WEB-INF/class/[/color]目录下,完整的目录结构复制过来,然后在把两个wsdd文件复制到[color=red]%tomcat_home%/axis/WEB-INF/[/color]目录下,打开控制台进入%tomcat_home%/axis/WEB-INF/目录下:
[color=blue]>java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -p 8082 deploy.wsdd [/color]
-s参数指定了AxisServlet所在的应用程序路径
[color=blue]>java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -p 8082 -s /axis/servlet/AxisServlet deploy.wsdd [/color]
-l参数指定目标应用的URL
[color=blue]>java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient –lhttp://localhost:8082/axis/services/MessageService deploy.wsdd [/color]

这个命令就是发布这个服务,发布成功后在控制台下会有提示:
Processing file deploy.wsdd
<Admin>Done processing</Admin>
同时发布后会在%tomcat_home%/axis/目录下,多了一个server-config.wsdd文件.
在浏览器输入:http://localhost:8082/axis/services/MessageService会看到下图
[img]http://dl.iteye.com/upload/attachment/259439/6c7b46c1-20e8-3334-9c7d-d7bf1585e43a.jpg[/img]
5.client的生成方法:
打开控制台进入[color=red]%tomcat_home%/axis/WEB-INF/[/color]目录下:
[color=blue]>java -Djava.ext.dirs=lib org.apache.axis.wsdl.WSDL2Java -p client http://localhost:8082/axis/services/MessageService?wsdl[/color]
会在当前的目录下生成client文件夹,这个目录里文件就是客户端源码。

6.通过WSDD文件卸载发布的webservice:
打开控制台进入[color=red]%tomcat_home%/axis/WEB-INF/[/color]目录下:
[color=blue]>java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient undeploy.wsdd [/color]
如果不是默认8080端口需要加上参数-p:
[color=blue]>java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -p 8082 undeploy.wsdd [/color]

[color=red]PS:java -Djava.ext.dirs=lib 也可以用java -cp "lib\*"[/color]
7.客户端Stubs方式实现如下:
import java.net.URL;

import javax.xml.namespace.QName;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import org.apache.axis.encoding.ser.BeanSerializerFactory;

import stubclient.UserBean;

/**
* test stub client
* @author Michael sun
*/
public class TestStubClient {

/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
QName qname = new QName("http://wsaxis.michael.com", "user");
Service s = new Service();
Call call = (Call) s.createCall();
// 注册这个bean为可序列化的.传递参数
call.registerTypeMapping(UserBean.class, qname,
new BeanSerializerFactory(UserBean.class, qname),
new BeanDeserializerFactory(UserBean.class, qname));
// 设置一下调用方法名.
call.setOperationName("getBeanStr");

// 设置一下这个服务的绝对路径.
call.setTargetEndpointAddress(new URL(
"http://localhost:8082/axis/services/MessageService?wsdl"));
// 实例化一个UserBean,这个UserBean是生成client的UserBean
UserBean u = new UserBean();
u.setAge(28);
u.setUserName("Michael");
// 通知方法,并返回结果
String str = (String) call.invoke(new Object[] { u });

System.out.println("web service 返回信息:" + str);

}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值