大家好,欢迎来到IT知识分享网。
在之前的学习中我们已经知道,axis是目前开发Web Service的主流框架之一。本文我们就来看看如何使用axis开发Web Service。
首先下载axis1.4包和tomcat服务器,并将解压后的axis1.4包下面的webapps下的axis目录复制到tomcat服务器的webapps文件夹中。axis支持三种Web Service的部署和开发,分别为:
a.Dynamic Invocation Interface(DII)
b.Stubs 方式
c.Dynamic Proxy方式
下面是使用axis开发Web Service对应的具体流程:
1.编写DII(Dynamic Invocation Interface)方式Web Service
a.编写服务程序HelloClient.java
[c-sharp] view plaincopy
public class HelloClient
{
public String getName(String name){
return “hello,” + name;
}
}
[c-sharp] view plaincopy
public class HelloClient
{
public String getName(String name){
return “hello,” + name;
}
}
b.将HelloClient.java文件拷贝到axis_home下,重命名为HelloClient.jws.
c.访问链接http://localhost:8080/axis/HelloClient.jws?wsdl页面显示axis自动生成的wsdl文件
d.编写访问服务的客户端TestHelloClient.java需要导入相应的axis.jar包,在下载的axis的WEB-INF/lib/目录下。
[c-sharp] view plaincopy
package com.yjpeng.webservice;
import java.net.URL;
import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class TestHelloClient {
public static void main(String[] args){
try{
String endpoint = “http://localhost:8080/axis/HelloClient.jws”;
Service service = new Service();
Call call = (Call)service.createCall();
call.setOperationName(new QName(endpoint, “getName”));
call.setTargetEndpointAddress(new URL(endpoint));
String result = (String) call.invoke(new Object[]{“张三”});
System.out.println(result);
}catch (Exception e) {
e.printStackTrace();
}
}
}
[c-sharp] view plaincopy
package com.yjpeng.webservice;
import java.net.URL;
import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class TestHelloClient {
public static void main(String[] args){
try{
String endpoint = “http://localhost:8080/axis/HelloClient.jws”;
Service service = new Service();
Call call = (Call)service.createCall();
call.setOperationName(new QName(endpoint, “getName”));
call.setTargetEndpointAddress(new URL(endpoint));
String result = (String) call.invoke(new Object[]{“张三”});
System.out.println(result);
}catch (Exception e) {
e.printStackTrace();
}
}
}
运行TestHelloClient.java在控制台输出hell,张三,测试成功。
2.编写Dynamci Proxy方式访问服务
a.编写部署服务端程序,用上边DII方式使用的HelloClient.java
[c-sharp] view plaincopy
public class HelloClient
{
public String getName(String name){
return “hello,” + name;
}
}
[c-sharp] view plaincopy
public class HelloClient
{
public String getName(String name){
return “hello,” + name;
}
}
b.编写代理接口HelloClientInterface.java需要扩展java.rmi.Remote类
[c-sharp] view plaincopy
package com.yjpeng.dynamic.proxy;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface HelloClientInterface extends Remote {
public String getName(String name) throws RemoteException;
}
[c-sharp] view plaincopy
package com.yjpeng.dynamic.proxy;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface HelloClientInterface extends Remote {
public String getName(String name) throws RemoteException;
}
c.编写访问服务的客户端TestHelloClient.java
[c-sharp] view plaincopy
package com.yjpeng.dynamic.proxy;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;
public class TestHelloClient {
public static void main(String[] args){
try{
String wsdlUrl = “http://localhost:8080/axis/HelloClient.jws?wsdl”;
String nameSpaceUrl = “http://localhost:8080/axis/HelloClient.jws”;
String serviceName = “HelloClientService”;
String portName = “HelloClient”;
ServiceFactory serviceFactory = ServiceFactory.newInstance();
Service afService = serviceFactory.createService(new URL(wsdlUrl), new QName(nameSpaceUrl, serviceName));
HelloClientInterface proxy = (HelloClientInterface)afService.getPort(new QName(nameSpaceUrl, portName),
HelloClientInterface.class);
System.out.println(proxy.getName(“张三”));
}catch (Exception e) {
e.printStackTrace();
}
}
}
[c-sharp] view plaincopy
package com.yjpeng.dynamic.proxy;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;
public class TestHelloClient {
public static void main(String[] args){
try{
String wsdlUrl = “http://localhost:8080/axis/HelloClient.jws?wsdl”;
String nameSpaceUrl = “http://localhost:8080/axis/HelloClient.jws”;
String serviceName = “HelloClientService”;
String portName = “HelloClient”;
ServiceFactory serviceFactory = ServiceFactory.newInstance();
Service afService = serviceFactory.createService(new URL(wsdlUrl), new QName(nameSpaceUrl, serviceName));
HelloClientInterface proxy = (HelloClientInterface)afService.getPort(new QName(nameSpaceUrl, portName),
HelloClientInterface.class);
System.out.println(proxy.getName(“张三”));
}catch (Exception e) {
e.printStackTrace();
}
}
}
运行TestHelloClient.java在控制台输出hell,张三,测试成功。
看完了本文,相信小伙伴们已经学会了如何使用axis开发Web Service,像这样的硬货在本站的Java教程中比比皆是,不容错过哦。
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/45317.html