服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

3步把您的java程序转换为webservice


  1、选择要转换的java文件,生成class
  2、写wsdd
  3、发布
  
  剩下的就只有调用了wsdl2java
  
  我原来的系统是cics的,对后台封装了一层,现在用webservice再封装一层,前台页面,控制,数据传输,数据处理统统都可以分开了,爽
  
  //以下是从网上找的关于axis的入门教程
  
  一、axis安装 1、环境 j2se sdk 1.3 or 1.4: 我使用 1.4.2 servlet container: 我使用的tomcat 5.0
  

  2、到 http://ws.apache.org/axis/网站下载axis安装包
  
  3、解压缩安装包,将axis_unzip_path/axis-version/webapps下的axis包拷贝到tomcat_home/webapps/下,以下约定axis_home为该tomcat_home/webapps/axis目录
  
  4、启动tomcat,访问http://localhost:8080/axis 检查安装是否成功
  
  5、以上步骤执行成功,可以开发webservice例子了
  
  axis支持三种web service的部署和开发,分别为:
  
  1、dynamic invocation interface ( dii)
  
  2、stubs方式
  
  3、dynamic proxy方式
  
  二、编写dii(dynamic invocation interface )方式web服务
  
  1.编写服务端程序helloclient
  
  public class helloclient
  {
  public string getname(string name)
  {
  return "hello "+name;
  }
  }
  
  2、将源码拷贝到axis_home下,重命名为 helloclient.jws
  
  3、访问连接http://localhost:8080/axis/helloclient.jws?wsdl,页面显示axis自动生成的wsdl
  
  4、编写访问服务的客户端 testhelloclient.java
  
  import org.apache.axis.client.call;
  import org.apache.axis.client.service;
  import javax.xml.namespace.qname;
  import javax.xml.rpc.serviceexception;
  import java.net.malformedurlexception;
  import java.rmi.remoteexception;
  
  public class sayhelloclient2
  {
  public static void main(string[] args)
  {
  try
  {
  string endpoint =
  "http://localhost:8080/axis/helloclient.jws";
  
  service service = new service();
  call call = null;
  
  call = (call) service.createcall();
  
  call.setoperationname(new qname(
  "http://localhost:8080/axis/helloclient.jws",
  "getname"));
  call.settargetendpointaddress
  (new java.net.url(endpoint));
  
  string ret = (string) call.invoke(new object[]
  {"zhangsan"});
  system.out.println("return value is " + ret);
  }
  catch (exception ex)
  {
  ex.printstacktrace();
  }
  }
  }
  
  三、编写dynamic proxy方式访问服务
  
  1、编写部署服务端程序,同上边dii方式,本次仍使用上边部署的helloclient
  
  2、编写代理接口
  
  public interface helloclientinterface
  extends java.rmi.remote
  {
  public string getname(string name)
  throws java.rmi.remoteexception;
  }
  
  3、编写并执行客户端程序testhelloclient.java
  
  import javax.xml.rpc.service;
  import javax.xml.rpc.servicefactory;
  import java.net.url;
  import javax.xml.namespace.qname;
  
  public class testhelloclient
  {
  public static void main(string[] args)
  {
  try
  {
  string wsdlurl =
  "http://localhost:8080/axis/helloclient.jws?wsdl";
  string namespaceuri =
  "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(namespaceuri, servicename));
  helloclientinterface proxy = (helloclientinterface)
  afservice.getport(new qname(
  namespaceuri, portname),
  helloclientinterface.class);
  system.out.println
  ("return value is "+proxy.getname("john") ) ;
  }catch(exception ex)
  {
  ex.printstacktrace() ;
  }
  }
  }
  
  四、编写wsdd发布web服务,编写stub client访问web服务
  
  1、编写服务端程序server,sayhello.java,编译server.sayhello.java
  package server;
  public class sayhello
  {
  public string getname(string name)
  {
  return "hello "+name;
  }
  }
  
  2.编写loghandler.java
  import org.apache.axis.axisfault;
  import org.apache.axis.handler;
  import org.apache.axis.messagecontext;
  import org.apache.axis.handlers.basichandler;
  
  import java.util.date;
  
  public class loghandler
  extends basichandler
  {
  public void invoke
  (messagecontext msgcontext)
  throws axisfault
  {
  /** log an access each time
  we get invoked.
  */
  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、编写wsdd文件
  
  deploy.wsdd
  <deployment xmlns=
  "http://xml.apache.org/axis/wsdd/"
  xmlns:java=
  "http://xml.apache.org/axis/wsdd/providers/java">
  <handler name="print" type="java:loghandler"/>
  <service name="sayhello"
  provider="java:rpc">
  <requestflow>
  <handler type="print"/>
  </requestflow>
  <parameter name="classname"
  value="server.sayhello"/>
  <parameter name="allowedmethods"
  value="*"/>
  </service>
  </deployment>
  
  3、将编译后的文件拷贝到axis_home/web-inf/classes下,如:d:/tomcat/webapps/axis/web-inf/classes
  
  4、发布服务:
  
  java org.apache.axis.client.adminclient deploy.wsdd
  
  5、生成client stub文件
  
  a:方式1
  
  将sayhello.java拷贝到axis_home/下,重命名为sayhello.jws,
  
  执行下面的命令生存client stub
  
  java org.apache.axis.wsdl.wsdl2java
  -p client http://localhost:8080
  /axis/services/sayhello.jws?wsdl
  
  b:方式2
  
  执行如下命令生成sayhello.wsdl
  
  java org.apache.axis.wsdl.java2wsdl
  -osayhello.wsdl -lhttp://localhost:8080
  /axis/services/sayhello -nsayhello server.sayhello
  
  执行如下命令生成client stub
  
  java org.apache.axis.wsdl.wsdl2java
  sayhello.wsdl -p client
  
  生成的stub client文件列表为:
  
  1.sayhello.java
  
  2.sayhelloservice.java。
  
  3.sayhelloservicelocator.java
  
  4.sayhellosoapbindingstub.java
  
  6、编写客户端程序,编译并执行
  
  public class sayhelloclient
  {
  public static void main(string[] args)
  {
  try
  {
  sayhelloservice service = new client.
  sayhelloservicelocator();
  client.sayhello_porttype
  client = service.getsayhello();
  string retvalue=client.getname("zhangsan");
  system.out.println(retvalue);
  }
  catch (exception e)
  {
  system.err.println
  ("execution failed. exception: " + e);
  }
  }
  }
  
  您也可以写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.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"/>
  <handler name="print" type="java:stub.loghandler"/>
  
  <service name="sayhello" provider="java:rpc">
  <requestflow>
  <handler type="print"/>
  </requestflow>
  <parameter name="classname"  value="stub.server.sayhello"/>
  <parameter name="allowedmethods" value="*"/>
  </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>

扫描关注微信公众号