专注Java教育14年 全国咨询/投诉热线:400-8080-105
动力节点LOGO图
始于2009,口口相传的Java黄埔军校
首页 hot资讯 使用axis开发Web Service

使用axis开发Web Service

更新时间:2020-09-22 17:22:17 来源:动力节点 浏览642次

在之前的学习中我们已经知道,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教程中比比皆是,不容错过哦。


提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>