小菜毛毛技術(shù)分享

          與大家共同成長

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            164 Posts :: 141 Stories :: 94 Comments :: 0 Trackbacks

          常用鏈接

          留言簿(15)

          我參與的團隊

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          使用Axis編寫WebService比較簡單,就我的理解,WebService的實現(xiàn)代碼和編寫Java代碼其實沒有什么區(qū)別,主要是將哪些Java類發(fā)布為WebService。下面是一個從編寫測試例子到發(fā)布WebService,以及編寫測試代碼的過程介紹。

                本例子的WebService提供了兩個方法,分別是sayHello和sayHelloToPerson,第一個只是返回一個"Hello"字符串,沒 有參數(shù),第二個函數(shù)接受一個字符串作為參數(shù),返回"Hello 參數(shù)值",該例子比較簡單,但是清楚的說明了從編寫代碼到發(fā)布為WebService以及測試編寫好的WebService全過程。

          編寫服務(wù)代碼

                服務(wù)代碼提供了兩個函數(shù),分別為sayHello和sayHelloToPerson,源代碼如下:


          /*
           * File name: HelloService.java
           * 
           * Version: v1.0
           * 
           * Created on Aug 2, 2008 9:40:20 AM
           * 
           * Designed by Stephen
           * 
           * (c)Copyright 2008
           
          */

          package com.sinosoft.webservice;

          /**
           * 
          @author Stephen
           * 
           * Test web service
           
          */

          public class HelloService {
              
          /**
               * 不帶參數(shù)的函數(shù)
               * 
               * 
          @return 返回Hello字符串
               
          */

              
          public String sayHello() {
                  
          return "Hello";
              }


              
          /**
               * 帶參數(shù)的函數(shù)
               * 
               * 
          @param name
               *            名稱
               * 
          @return 返回加上名稱的歡迎詞
               
          */

              
          public String sayHelloToPerson(String name) {
                  
          if (name == null || name.equals("")) {
                      name 
          = "nobody";
                  }

                  
          return "Hello " + name;
              }

          }

          發(fā)布WebService

                要將上邊寫的HelloService類發(fā)布為WebService,需要先搭建Web應(yīng)用。下面是在Tomcat下使用Axis創(chuàng)建WebService服務(wù)的例子。

          在Tomcat下創(chuàng)建Web應(yīng)用

               在該例子中,在Tomcat下創(chuàng)建了一個context path為ws的WEB應(yīng)用。

               1. 在Tomcat的webapps下創(chuàng)建如下文件系統(tǒng)

                    ws

                         WEB-INF

                              lib

                              classes

               2. 在WEB-INF文件夾下創(chuàng)建web.xml文件,該文件的內(nèi)容如下:


          <?xml version="1.0" encoding="ISO-8859-1"?>

          <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web
          Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"
          >

          <web-app>
            
          <display-name>Apache-Axis</display-name>
              
              
          <listener>
                  
          <listener-class>org.apache.axis.transport.http.AxisHTTPSessionListener</listener-class>
              
          </listener>
              
            
          <servlet>
              
          <servlet-name>AxisServlet</servlet-name>
              
          <display-name>Apache-Axis Servlet</display-name>
              
          <servlet-class>
                  org.apache.axis.transport.http.AxisServlet
              
          </servlet-class>
            
          </servlet>

            
          <servlet>
              
          <servlet-name>AdminServlet</servlet-name>
              
          <display-name>Axis Admin Servlet</display-name>
              
          <servlet-class>
                  org.apache.axis.transport.http.AdminServlet
              
          </servlet-class>
              
          <load-on-startup>100</load-on-startup>
            
          </servlet>

            
          <servlet>
              
          <servlet-name>SOAPMonitorService</servlet-name>
              
          <display-name>SOAPMonitorService</display-name>
              
          <servlet-class>
                  org.apache.axis.monitor.SOAPMonitorService
              
          </servlet-class>
              
          <init-param>
                
          <param-name>SOAPMonitorPort</param-name>
                
          <param-value>5001</param-value>
              
          </init-param>
              
          <load-on-startup>100</load-on-startup>
            
          </servlet>

            
          <servlet-mapping>
              
          <servlet-name>AxisServlet</servlet-name>
              
          <url-pattern>/servlet/AxisServlet</url-pattern>
            
          </servlet-mapping>

            
          <servlet-mapping>
              
          <servlet-name>AxisServlet</servlet-name>
              
          <url-pattern>*.jws</url-pattern>
            
          </servlet-mapping>

            
          <servlet-mapping>
              
          <servlet-name>AxisServlet</servlet-name>
              
          <url-pattern>/services/*</url-pattern>
            
          </servlet-mapping>

            
          <servlet-mapping>
              
          <servlet-name>SOAPMonitorService</servlet-name>
              
          <url-pattern>/SOAPMonitor</url-pattern>
            
          </servlet-mapping>

           
          <!-- uncomment this if you want the admin servlet -->
           
          <!--
            <servlet-mapping>
              <servlet-name>AdminServlet</servlet-name>
              <url-pattern>/servlet/AdminServlet</url-pattern>
            </servlet-mapping>
           
          -->

              
          <session-config>
                  
          <!-- Default to 5 minute session timeouts -->
                  
          <session-timeout>5</session-timeout>
              
          </session-config>

              
          <!-- currently the W3C havent settled on a media type for WSDL;
              http://www.w3.org/TR/2003/WD-wsdl12-20030303/#ietf-draft
              for now we go with the basic 'it's XML' response 
          -->
            
          <mime-mapping>
              
          <extension>wsdl</extension>
               
          <mime-type>text/xml</mime-type>
            
          </mime-mapping>
            

            
          <mime-mapping>
              
          <extension>xsd</extension>
              
          <mime-type>text/xml</mime-type>
            
          </mime-mapping>

            
          <welcome-file-list id="WelcomeFileList">
              
          <welcome-file>index.jsp</welcome-file>
              
          <welcome-file>index.html</welcome-file>
              
          <welcome-file>index.jws</welcome-file>
            
          </welcome-file-list>

          </web-app>

               在上面的web.xml中主要是配置了axis的相關(guān)配置。

          axis的相關(guān)配置

               在上述的web.xml文件中已經(jīng)對axis進行了配置,但是還需要進行額外的配置。

               復(fù)制axis相關(guān)的jar文件

               將axis的相關(guān)jar文件復(fù)制到WEB-INF"lib文件夾下。這些文件包括:

          activation.jar
          axis.jar
          axis-ant.jar
          axis-schema.jar
          commons-discovery-0.2.jar
          commons-logging-1.0.4.jar
          jaxrpc.jar
          log4j-1.2.8.jar
          mailapi.jar
          saaj.jar
          wsdl4j-1.5.1.jar
          xmlsec-1.3.0.jar

               復(fù)制WebService服務(wù)主文件

               將HelloService.java編譯后的class文件復(fù)制到WEB-INF"classes文件夾下,也就是說在WEB-INF "classes文件夾下的文件夾結(jié)構(gòu)為:com"sinosoft"webservice,在webservice文件夾下有一個 helloservice.class文件。

          測試發(fā)布的Web應(yīng)用

               啟動Tomcat服務(wù),打開IE瀏覽器,訪問地址http:host:port/ws/services,如果看到如下界面就說明AXIS部署成功了。

          發(fā)布WebService

               發(fā)布WebService需要使用現(xiàn)有的AdminService來實現(xiàn),這里我寫了一個批處理文件來發(fā)布WebService,以后如果需要發(fā)布其他文件,只需要修改相應(yīng)的參數(shù)就可以了。

          創(chuàng)建deploy.wsdd文件

               文件deploy.wsdd內(nèi)容如下所示:


          <?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">
              
          <service name="HelloServices" provider="java:RPC">
                  
          <parameter name="className" value="com.sinosoft.webservice.HelloService"/>
                  
          <parameter name="allowedMethods" value="*"/>
              
          </service>
          </deployment>

          創(chuàng)建發(fā)布WebService服務(wù)的批處理文件

               批處理文件deploywebservice.bat內(nèi)容如下:


          java -cp E:"Stephen"Lib"axislib"activation.jar;E:"Stephen"Lib"axislib"axis-ant.jar;E:"Stephen"Lib"axislib"axis-schema.jar;E:"Stephen"Lib"axislib"axis.jar;E:"Stephen"Lib"axislib"commons-discovery-0.2.jar;E:"Stephen"Lib"axislib"commons-logging-1.0.4.jar;E:"Stephen"Lib"axislib"jaxrpc.jar;E:"Stephen"Lib"axislib"log4j-1.2.8.jar;E:"Stephen"Lib"axislib"mailapi.jar;E:"Stephen"Lib"axislib"saaj.jar;E:"Stephen"Lib"axislib"wsdl4j-1.5.1.jar;E:"Stephen"Lib"axislib"xmlsec-1.3.0.jar org.apache.axis.client.AdminClient -lhttp://localhost:8090/ws/services/AdminService deploy.wsdd

               其中E:"Stephen"Lib"axislib是存放axis對應(yīng)的jar文件的文件夾,現(xiàn)在將所有的jar文件都加入到classpath中進行執(zhí)行。

               -l后的參數(shù)是本地要發(fā)布WebService的AdminService對應(yīng)的訪問地址。

               最后deploy.wsdd是對應(yīng)的配置文件名稱。

          發(fā)布WebService服務(wù)

               將deploy.wsdd文件和deploywebservice.bat文件復(fù)制到同一個文件夾下,執(zhí)行deploywebservice.bat批處理文件,就可以將deploy.wsdd中描述的Java類發(fā)布為WebService。發(fā)布完成之后在訪問http://host:port/ws/services如下圖所示:

           

          從上圖可以看出,發(fā)布成功后,多了一個HelloServices的服務(wù)。這樣就說明HelloService發(fā)布成功了。

          查看HelloServices的wsdl

               訪問http://host:port/ws/services/HelloServices?wsdl可以看到如下wsdl的內(nèi)容:


          <?xml version="1.0" encoding="UTF-8"?>
          <wsdl:definitions targetNamespace="http://localhost:8090/ws2/services/HelloServices" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://localhost:8090/ws2/services/HelloServices" xmlns:intf="http://localhost:8090/ws2/services/HelloServices" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
          <!--WSDL created by Apache Axis version: 1.3
          Built on Oct 05, 2005 (05:23:37 EDT)
          -->

             
          <wsdl:message name="sayHelloResponse">

                
          <wsdl:part name="sayHelloReturn" type="soapenc:string"/>

             
          </wsdl:message>

             
          <wsdl:message name="sayHelloToPersonResponse">

                
          <wsdl:part name="sayHelloToPersonReturn" type="soapenc:string"/>

             
          </wsdl:message>

             
          <wsdl:message name="sayHelloToPersonRequest">

                
          <wsdl:part name="name" type="soapenc:string"/>

             
          </wsdl:message>

             
          <wsdl:message name="sayHelloRequest">

             
          </wsdl:message>

             
          <wsdl:portType name="HelloService">

                
          <wsdl:operation name="sayHello">

                   
          <wsdl:input message="impl:sayHelloRequest" name="sayHelloRequest"/>

                   
          <wsdl:output message="impl:sayHelloResponse" name="sayHelloResponse"/>

                
          </wsdl:operation>

                
          <wsdl:operation name="sayHelloToPerson" parameterOrder="name">

                   
          <wsdl:input message="impl:sayHelloToPersonRequest" name="sayHelloToPersonRequest"/>

                   
          <wsdl:output message="impl:sayHelloToPersonResponse" name="sayHelloToPersonResponse"/>

                
          </wsdl:operation>

             
          </wsdl:portType>

             
          <wsdl:binding name="HelloServicesSoapBinding" type="impl:HelloService">

                
          <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>

                
          <wsdl:operation name="sayHello">

                   
          <wsdlsoap:operation soapAction=""/>

                   
          <wsdl:input name="sayHelloRequest">

                      
          <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://webservice.sinosoft.com" use="encoded"/>

                   
          </wsdl:input>

                   
          <wsdl:output name="sayHelloResponse">

                      
          <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost:8090/ws2/services/HelloServices" use="encoded"/>

                   
          </wsdl:output>

                
          </wsdl:operation>

                
          <wsdl:operation name="sayHelloToPerson">

                   
          <wsdlsoap:operation soapAction=""/>

                   
          <wsdl:input name="sayHelloToPersonRequest">

                      
          <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://webservice.sinosoft.com" use="encoded"/>

                   
          </wsdl:input>

                   
          <wsdl:output name="sayHelloToPersonResponse">

                      
          <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost:8090/ws2/services/HelloServices" use="encoded"/>

                   
          </wsdl:output>

                
          </wsdl:operation>

             
          </wsdl:binding>

             
          <wsdl:service name="HelloServiceService">

                
          <wsdl:port binding="impl:HelloServicesSoapBinding" name="HelloServices">

                   
          <wsdlsoap:address location="http://localhost:8090/ws2/services/HelloServices"/>

                
          </wsdl:port>

             
          </wsdl:service>

          </wsdl:definitions>

          用Java調(diào)用WebService實例

               下面是用Java調(diào)用剛發(fā)布的WebService例子。


          /*
           * File name: TestHelloService.java
           * 
           * Version: v1.0
           * 
           * Created on Aug 2, 2008 9:54:10 AM
           * 
           * Designed by Stephen
           * 
           * (c)Copyright 2008
           
          */
          package test.com.sinosoft.webservice;

          import java.io.IOException;
          import java.net.MalformedURLException;

          import javax.xml.namespace.QName;
          import javax.xml.rpc.ServiceException;

          import org.apache.axis.client.Call;
          import org.apache.axis.client.Service;
          import org.apache.commons.logging.Log;
          import org.apache.commons.logging.LogFactory;

          /**
           * 
          @author Stephen
           * 
           * 測試調(diào)用WebService
           
          */
          public class TestHelloService {
              
          private static final Log log = LogFactory.getLog(TestHelloService.class);
              
          private static final String HELLO_SERVICE_ENDPOINT = "http://localhost:8090/ws/services/HelloServices?wsdl";

              
          public static void main(String[] args) {
                  TestHelloService tester 
          = new TestHelloService();
                  
          // tester.callSayHello();
                  tester.callSayHelloToPerson();
              }

              
          public void callSayHello() {
                  
          try {
                      Service service 
          = new Service();
                      Call call 
          = (Call) service.createCall();
                      call.setTargetEndpointAddress(
          new java.net.URL(
                              HELLO_SERVICE_ENDPOINT));
                      call.setOperationName(
          new QName("http://webservice.sinosoft.com/",
                              
          "sayHello"));
                      call.setReturnType(org.apache.axis.Constants.XSD_STRING);
                      
          try {
                          String ret 
          = (String) call.invoke(new Object[] {});
                          System.out.println(
          "The return value is:" + ret);
                          
          return;
                      } 
          catch (IOException e) {
                          e.printStackTrace();
                      }
                  } 
          catch (MalformedURLException e) {
                      e.printStackTrace();
                  } 
          catch (ServiceException e) {
                      e.printStackTrace();
                  }
                  log.error(
          "call sayHello service error!");
              }

              
          public void callSayHelloToPerson() {
                  
          try {
                      Service service 
          = new Service();
                      Call call 
          = (Call) service.createCall();
                      call.setTargetEndpointAddress(
          new java.net.URL(
                              HELLO_SERVICE_ENDPOINT));
                      call.setOperationName(
          new QName("http://webservice.sinosoft.com/",
                              
          "sayHelloToPerson"));
                      call.addParameter(
          "name", org.apache.axis.Constants.XSD_STRING,
                              javax.xml.rpc.ParameterMode.IN);
                      call.setReturnType(org.apache.axis.Constants.XSD_STRING);
                      
          try {
                          String ret 
          = (String) call.invoke(new Object[] { "Stephen" });
                          System.out.println(
          "The return value is:" + ret);
                          
          return;
                      } 
          catch (IOException e) {
                          e.printStackTrace();
                      }
                  } 
          catch (MalformedURLException e) {
                      e.printStackTrace();
                  } 
          catch (ServiceException e) {
                      e.printStackTrace();
                  }
                  log.error(
          "call sayHello service error!");
              }
          }
          posted on 2010-10-09 17:41 小菜毛毛 閱讀(13405) 評論(2)  編輯  收藏 所屬分類: webservice

          Feedback

          # tengxun 2014-11-05 15:08 nht
          的份兒幅度由IDF物品吃的降低額外繳費  回復(fù)  更多評論
            

          # re: 利用Java編寫簡單的WebService實例[未登錄] 2015-07-14 11:31 111
          11  回復(fù)  更多評論
            

          主站蜘蛛池模板: 洛浦县| 靖远县| 通州市| 囊谦县| 沙湾县| 安西县| 尉氏县| 夏河县| 淮滨县| 昌宁县| 山东省| 锡林郭勒盟| 哈密市| 益阳市| 克山县| 边坝县| 行唐县| 宿松县| 巴彦淖尔市| 丰台区| 科技| 凉城县| 漠河县| 沾化县| 瑞金市| 宜丰县| 石嘴山市| 庆城县| 巩义市| 迁西县| 罗田县| 宁津县| 沛县| 务川| 凌云县| 那曲县| 肇州县| 霞浦县| 呈贡县| 荔波县| 绥棱县|