3步把您的java程式轉換為webservice

來源:互聯網
上載者:User

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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.