java中webservice的使用实例

一、Axis安装 1、环境 J2SE SDK 1.5 or 1.6: 我使用 1.6 Servlet Container: 我使用的Tomcat 6.0

2、到 http://ws.apache.org/Axis/网站下载Axis安装包

3、先在eclipse下新建web project为WebServiceDemo.在Tomcat的配置xml配置上:<Context path="/webservice" docBase="D:/workspace/WebServiceDemo/WebRoot"/>.解压下载的axis安装包,将webapps/axis下的lib文件夹和web.xml文件拷到工程对应目录

4、启动tomcat,访问 http://localhost/webservice检查安装是否成功,我的端口设置为80,页面index.html自动跳转后出错,是因为没把类拷入,先不管。

5、以上步骤执行成功,可以开发webservice例子了

Axis支持三种web service的部署和开发,分别为:

1、Dynamic Invocation Interface ( DII)

2、Stubs方式

3、Dynamic Proxy方式

二、编写DII(Dynamic Invocation Interface )方式web服务

1.新建包com.qdl.server,编写服务端程序SayHello
package com.qdl.server;

public class SayHello
{
      public String getName(String name)
      {
          return "hello "+name;
      }
}


2、将源码拷贝到WebRoot下,重命名为 SayHello.jws (注意不用拷上面的package ...行)

3、访问连接 http://localhost/webservice/SayHello.jws?wsdl,页面显示Axis自动生成的wsdl,同时会在WEB-INF下创建jwsClasses文件夹,在此生成SayHello.class文件

4、新建包com.qdl.client,编写访问服务的客户端 SayHelloClient.java

import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class SayHelloClient {
 public static void main(String[] args){
  //程序访问即时发布的服务
  try{
   //创建访问点
   String endpoint = " http://localhost/webservice/SayHello.jws";
   //创建service
   Service service = new Service();
   Call call = null;
   //创建call
   call = (Call) service.createCall();
   //设置操作名
   call.setOperationName(new QName(" http://localhost/webservice/SayHello.jws","getName"));
   //设置访问点
   call.setTargetEndpointAddress(new java.net.URL(endpoint));
   //调用服务,返回值
   String ret = (String) call.invoke(new Object[]{"zhangsan"});
   System.out.println("返回的信息是:" + ret);
  }catch (Exception ex){
   ex.printStackTrace();
  }
 }
}
三、编写Dynamic Proxy方式访问服务

1、编写部署服务端程序,同上边DII方式,本次仍使用上边部署的SayHello

2、新建包com.qdl.dao编写代理接口

package com.qdl.dao;
import java.rmi.Remote;
public interface HelloClientInterface extends Remote {
 
 public String getName(String name) throws java.rmi.RemoteException;
}
3、编写并执行客户端程序TestHelloClient.java

package com.qdl.client;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceFactory;
import javax.xml.rpc.Service;
import com.qdl.dao.HelloClientInterface;
public class TestHelloClient {
 public static void main(String[] args) {
  try {
   String wsdlUrl = " http://localhost/webservice/SayHello.jws?wsdl";
   String nameSpaceUri = " http://localhost/webservice/SayHello.jws";
   //注意下面的serviceName和portName,
   //http://localhost/webservice/SayHello.jws?wsdl页面的xml最下端有
   String serviceName = "SayHelloService";
   String portName = "SayHello";
   ServiceFactory serviceFactory = ServiceFactory.newInstance();
   Service afService = serviceFactory.createService(new URL(wsdlUrl), new QName(nameSpaceUri, serviceName));
   HelloClientInterface proxy = (HelloClientInterface) afService.getPort(new QName(nameSpaceUri, portName), HelloClientInterface.class);
   System.out.println("返回信息是:" + proxy.getName("zhangsan"));
  } catch (Exception ex) {
   ex.printStackTrace();
  }
 }
}

四、编写wsdd发布web服务,编写stub client访问web服务(定制发布服务)

1、编写服务端程序SayHello
package com.qdl.server;

public class SayHello
{
      public String getName(String name)
      {
          return "hello "+name;
      }
}
2.编写LogHandler.java(好像这个并没什么用)

import java.util.Date;
import org.apache.axis.AxisFault;
import org.apache.axis.Handler;
import org.apache.axis.MessageContext;
import org.apache.axis.handlers.BasicHandler;
public class LogHandler extends BasicHandler {
 public void invoke(MessageContext msgContext) throws AxisFault {
  try {
   Handler serviceHandler = msgContext.getService();
   Integer numAccesses = (Integer) serviceHandler.getOption("accesses");
   if (numAccesses == null) {
    numAccesses = new Integer(0);
   }
   numAccesses = new Integer(numAccesses.intValue() + 1);
   Date date = new Date();
   String result = date + ": service " + msgContext.getTargetService() + " accessed " + numAccesses + " time(s).";
   serviceHandler.setOption("accesses", numAccesses);
   System.out.println(result);
  } catch (Exception e) {
   throw AxisFault.makeFault(e);
  }
 }
}


3、先在WEB-INF目录下编写server-config.wsdd文件:
文件名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="adminPassword" value="admin"/>
  <parameter name="attachments.Directory" value="./attachments"/>
  <parameter name="attachments.implementation"
             value="org.apache.axis.attachments.AttachmentsImpl"/>
  <parameter name="sendXsiTypes" value="true"/>
  <parameter name="sendMultiRefs" value="true"/>
  <parameter name="sendXMLDeclaration" value="true"/>
  <parameter name="axis.sendMinimizedElements" value="true"/>
  <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="LocalResponder"
          type="java:org.apache.axis.transport.local.LocalResponder"/>
 <handler name="URLMapper"
          type="java:org.apache.axis.handlers.http.URLMapper"/>
 <handler name="Authenticate"
          type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/>
 <service name="AdminService" provider="java:MSG">
  <parameter name="allowedMethods" value="AdminService"/>
  <parameter name="enableRemoteAdmin" value="false"/>
  <parameter name="className" value="org.apache.axis.utils.Admin"/>
  <namespace>http://xml.apache.org/axis/wsdd/</namespace>
 </service>
 <service name="Version" provider="java:RPC">
  <parameter name="allowedMethods" value="getVersion"/>
  <parameter name="className" value="org.apache.axis.Version"/>
 </service>
<transport name="http">
  <requestFlow>
   <handler type="URLMapper"/>
   <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>
  </requestFlow>
 </transport>
 <transport name="local">
  <responseFlow>
   <handler type="LocalResponder"/>
  </responseFlow>
 </transport>
</deployment>
 
 
然后在WEB-INF下再编写deploy.wsdd文件:
文件名deploy.wsdd

<deployment xmlns=" http://xml.apache.org/axis/wsdd/" xmlns:java=" http://xml.apache.org/axis/wsdd/providers/java">
 <service name="MyService" provider="java:RPC">
  <parameter name="className" value="com.qdl.server.SayHello"/>
  <parameter name="allowedMethods" value="*"/>
 </service>
</deployment>

4、发布服务:

java org.apache.axis.client.AdminClient -lhttp://地址:端口/context模块/services/AdminService deploy.wsdd
 
这一命令主要是把deploy.wsdd的service配置到server-config.wsdd中,同时更新内存
 
注意:上面命令的前提是要把axis的jar包配置到系统classpath环境变量中,否则报找不到类的错误

5、生成client stub文件

a:方式1(本人用得此种方式)

将SayHello.java拷贝到WebRoot下(前面已经拷贝),重命名为SayHello.jws,

执行下面的命令生存client stub

java org.apache.axis.wsdl.WSDL2Java -p com.qdl.client  http://localhost/webservice/SayHello.jws?wsdl


b:方式2

执行如下命令生成SayHello.wsdl

java org.apache.axis.wsdl.Java2WSDL -oSayHello.wsdl -lhttp://localhost/webservice/SayHello -nsayhello com.qdl.server.SayHello

执行如下命令生成client stub

java org.apache.axis.wsdl.WSDL2Java SayHello.wsdl -p com.qdl.client 

此时在文件夹WEB-INF下com.qdl.client下生成的stub client文件列表为:

1.SayHello.java

2.SayHelloService.java。

3.SayHelloServiceLocator.java

4.SayHelloSoapBindingStub.java
 
把com包整体剪到src下编译


6、编写客户端程序,编译并执行

package com.qdl.client;
 
public class SayHelloClient2 {
 public static void main(String[] args){
  //访问定制发布的服务
  try{
   SayHelloService service = new SayHelloServiceLocator();
   SayHello_PortType client = service.getSayHello();
   String retValue = client.getName("zhangsan");
   System.out.println("返回的信息是:"+retValue);
  }catch (Exception e){
   System.err.println("Execution failed. Exception: " + e);
  }
 }
}
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值