<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    小菜毛毛技術分享

    與大家共同成長

      BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
      164 Posts :: 141 Stories :: 94 Comments :: 0 Trackbacks
    使用Axis編寫WebService比較簡單,就我的理解,WebService的實現代碼和編寫Java代碼其實沒有什么區別,主要是將哪些Java類發布為WebService。下面是一個從編寫測試例子到發布WebService,以及編寫測試代碼的過程介紹。

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

    編寫服務代碼

          服務代碼提供了兩個函數,分別為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 {
        
    /**
         * 不帶參數的函數
         * 
         * 
    @return 返回Hello字符串
         
    */

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


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

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

            
    return "Hello " + name;
        }

    }

    發布WebService

          要將上邊寫的HelloService類發布為WebService,需要先搭建Web應用。下面是在Tomcat下使用Axis創建WebService服務的例子。

    在Tomcat下創建Web應用

         在該例子中,在Tomcat下創建了一個context path為ws的WEB應用。

         1. 在Tomcat的webapps下創建如下文件系統

              ws

                   WEB-INF

                        lib

                        classes

         2. 在WEB-INF文件夾下創建web.xml文件,該文件的內容如下:


    <?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的相關配置。

    axis的相關配置

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

         復制axis相關的jar文件

         將axis的相關jar文件復制到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

         復制WebService服務主文件

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

    測試發布的Web應用

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

    發布WebService

         發布WebService需要使用現有的AdminService來實現,這里我寫了一個批處理文件來發布WebService,以后如果需要發布其他文件,只需要修改相應的參數就可以了。

    創建deploy.wsdd文件

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

    創建發布WebService服務的批處理文件

         批處理文件deploywebservice.bat內容如下:


    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對應的jar文件的文件夾,現在將所有的jar文件都加入到classpath中進行執行。

         -l后的參數是本地要發布WebService的AdminService對應的訪問地址。

         最后deploy.wsdd是對應的配置文件名稱。

    發布WebService服務

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

     

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

    查看HelloServices的wsdl

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


    <?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調用WebService實例

         下面是用Java調用剛發布的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
     * 
     * 測試調用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 小菜毛毛 閱讀(13402) 評論(2)  編輯  收藏 所屬分類: webservice

    Feedback

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

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

    主站蜘蛛池模板: 成年女人18级毛片毛片免费观看| 国产精品高清视亚洲精品| 青青青国产免费一夜七次郎| 青青青国产手机频在线免费观看| 偷自拍亚洲视频在线观看| 亚洲a∨无码男人的天堂| 亚洲av伊人久久综合密臀性色| 亚洲七七久久精品中文国产| 免费无码又爽又刺激高潮| 国产福利视精品永久免费| 久久午夜夜伦鲁鲁片无码免费| 一区二区三区免费高清视频| 国产亚洲综合久久| 亚洲第一成年网站视频| 亚洲乱码在线播放| 亚洲精品无码久久毛片波多野吉衣| 亚洲人精品午夜射精日韩| 亚洲综合区小说区激情区| 免费少妇a级毛片人成网| 午夜免费福利影院| 啦啦啦手机完整免费高清观看| 欧亚精品一区三区免费| 日韩av无码成人无码免费| 91短视频免费在线观看| **一级一级毛片免费观看| 中文字幕视频免费| 99re6在线视频精品免费下载| 久久免费国产视频| 免费人成在线观看网站品爱网| 久久国产乱子伦精品免费看| 色欲A∨无码蜜臀AV免费播| 久久爰www免费人成| 97av免费视频| 免费下载成人电影| 午夜精品在线免费观看| 国产成人青青热久免费精品| 免费在线观看你懂的| 亚洲一区二区高清| 亚洲国产精品无码中文字| 亚洲91av视频| 亚洲欧洲日产国码在线观看|