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

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

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

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

    與大家共同成長(zhǎng)

      BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
      164 Posts :: 141 Stories :: 94 Comments :: 0 Trackbacks
    使用Axis編寫WebService比較簡(jiǎn)單,就我的理解,WebService的實(shí)現(xiàn)代碼和編寫Java代碼其實(shí)沒(méi)有什么區(qū)別,主要是將哪些Java類發(fā)布為WebService。下面是一個(gè)從編寫測(cè)試?yán)拥桨l(fā)布WebService,以及編寫測(cè)試代碼的過(guò)程介紹。

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

    編寫服務(wù)代碼

          服務(wù)代碼提供了兩個(gè)函數(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)建了一個(gè)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)對(duì)axis進(jìn)行了配置,但是還需要進(jìn)行額外的配置。

         復(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文件夾下,也就是說(shuō)在WEB-INF "classes文件夾下的文件夾結(jié)構(gòu)為:com"sinosoft"webservice,在webservice文件夾下有一個(gè) helloservice.class文件。

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

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

    發(fā)布WebService

         發(fā)布WebService需要使用現(xiàn)有的AdminService來(lái)實(shí)現(xiàn),這里我寫了一個(gè)批處理文件來(lái)發(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對(duì)應(yīng)的jar文件的文件夾,現(xiàn)在將所有的jar文件都加入到classpath中進(jìn)行執(zhí)行。

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

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

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

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

     

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

    查看HelloServices的wsdl

         訪問(wèn)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實(shí)例

         下面是用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
     * 
     * 測(cè)試調(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 小菜毛毛 閱讀(13402) 評(píng)論(2)  編輯  收藏 所屬分類: webservice

    Feedback

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

    # re: 利用Java編寫簡(jiǎn)單的WebService實(shí)例[未登錄](méi) 2015-07-14 11:31 111
    11  回復(fù)  更多評(píng)論
      

    主站蜘蛛池模板: 日韩免费高清大片在线| 免费无码又爽又高潮视频| 亚洲美日韩Av中文字幕无码久久久妻妇 | 美女被爆羞羞网站在免费观看| 99精品一区二区免费视频| 久久影视国产亚洲| 亚洲av成人无码网站…| 在线看免费观看AV深夜影院| 亚洲av无码国产精品夜色午夜| 国产成人综合久久精品亚洲| 成人网站免费观看| 亚洲精品高清国产麻豆专区| 免费看成人AA片无码视频吃奶| 亚洲成A人片77777国产| 亚洲av永久无码精品网址| 一二三四在线播放免费观看中文版视频| 亚洲av午夜福利精品一区| 一级做a爱片特黄在线观看免费看| 曰皮全部过程视频免费国产30分钟| 亚洲另类自拍丝袜第1页| 日韩精品久久久久久免费| 国产亚洲真人做受在线观看| jizz在线免费观看| 亚洲国产精品专区在线观看| 成人亚洲国产精品久久| 午夜无遮挡羞羞漫画免费| 亚洲一级毛片中文字幕| 日本最新免费网站| 亚洲精品综合久久中文字幕| 免费精品无码AV片在线观看| 久久久亚洲精品视频| 国产一二三四区乱码免费| 国产亚洲欧洲Aⅴ综合一区 | 国产91免费在线观看| 亚洲网站在线播放| 蜜桃成人无码区免费视频网站 | 三年片在线观看免费观看大全一 | 久操视频免费观看| 亚洲成AV人在线观看天堂无码| 中文永久免费观看网站| 亚洲色偷偷偷鲁综合|