• 利用Java编写简单的WebService实例


      使用Axis编写WebService比較简单,就我的理解,WebService的实现代码和编写Java代码事实上没有什么差别,主要是将哪些Java类公布为WebService。

    以下是一个从编写測试样例到公布WebService,以及编写測试代码的过程介绍。

          本样例的WebService提供了两个方法。各自是sayHello和sayHelloToPerson。第一个仅仅是返回一个"Hello"字符串,没有參数,第二个函数接受一个字符串作为參数。返回"Hello 參数值",该样例比較简单。可是清楚的说明了从编写代码到公布为WebService以及測试编写好的WebService全过程。

    编写服务代码

          服务代码提供了两个函数。分别为sayHello和sayHelloToPerson,源码例如以下:

    Java代码  收藏代码
    1. /* 
    2.  * File name: HelloService.java 
    3.  *  
    4.  * Version: v1.0 
    5.  *  
    6.  * Created on Aug 2, 2008 9:40:20 AM 
    7.  *  
    8.  * Designed by Stephen 
    9.  *  
    10.  * (c)Copyright 2008 
    11.  */  
    12. package com.sinosoft.webservice;  
    13.   
    14. /** *//** 
    15.  * @author Stephen 
    16.  *  
    17.  * Test web service 
    18.  */  
    19. public class HelloService {  
    20.     /** *//** 
    21.      * 不带參数的函数 
    22.      *  
    23.      * @return 返回Hello字符串 
    24.      */  
    25.     public String sayHello() {  
    26.         return "Hello";  
    27.     }  
    28.   
    29.     /** *//** 
    30.      * 带參数的函数 
    31.      *  
    32.      * @param name 
    33.      *            名称 
    34.      * @return 返回加上名称的欢迎词 
    35.      */  
    36.     public String sayHelloToPerson(String name) {  
    37.         if (name == null || name.equals("")) {  
    38.             name = "nobody";  
    39.         }  
    40.         return "Hello " + name;  
    41.     }  
    42. }  

     

     

    公布WebService

          要将上边写的HelloService类公布为WebService。须要先搭建Web应用。以下是在Tomcat下使用Axis(http://ws.apache.org/axis/)创建WebService服务的样例。

    在Tomcat下创建Web应用

    在该样例中,在Tomcat下创建了一个context path为ws的WEB应用。

         1. 在myeclipse中创建WebServiceTest的webproject

         2. 在WEB-INF目录下web.xml文件,该文件的内容例如以下:

    Xml代码  收藏代码
    1. <?xml version="1.0" encoding="ISO-8859-1"?

      >  

    2.   
    3. <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web  
    4. Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">  
    5.   
    6. <web-app>  
    7.   <display-name>Apache-Axis</display-name>  
    8.       
    9.     <listener>  
    10.         <listener-class>org.apache.axis.transport.http.AxisHTTPSessionListener</listener-class>  
    11.     </listener>  
    12.       
    13.   <servlet>  
    14.     <servlet-name>AxisServlet</servlet-name>  
    15.     <display-name>Apache-Axis Servlet</display-name>  
    16.     <servlet-class>  
    17.         org.apache.axis.transport.http.AxisServlet  
    18.     </servlet-class>  
    19.   </servlet>  
    20.   
    21.   <servlet>  
    22.     <servlet-name>AdminServlet</servlet-name>  
    23.     <display-name>Axis Admin Servlet</display-name>  
    24.     <servlet-class>  
    25.         org.apache.axis.transport.http.AdminServlet  
    26.     </servlet-class>  
    27.     <load-on-startup>100</load-on-startup>  
    28.   </servlet>  
    29.   
    30.   <servlet>  
    31.     <servlet-name>SOAPMonitorService</servlet-name>  
    32.     <display-name>SOAPMonitorService</display-name>  
    33.     <servlet-class>  
    34.         org.apache.axis.monitor.SOAPMonitorService  
    35.     </servlet-class>  
    36.     <init-param>  
    37.       <param-name>SOAPMonitorPort</param-name>  
    38.       <param-value>5001</param-value>  
    39.     </init-param>  
    40.     <load-on-startup>100</load-on-startup>  
    41.   </servlet>  
    42.   
    43.   <servlet-mapping>  
    44.     <servlet-name>AxisServlet</servlet-name>  
    45.     <url-pattern>/servlet/AxisServlet</url-pattern>  
    46.   </servlet-mapping>  
    47.   
    48.   <servlet-mapping>  
    49.     <servlet-name>AxisServlet</servlet-name>  
    50.     <url-pattern>*.jws</url-pattern>  
    51.   </servlet-mapping>  
    52.   
    53.   <servlet-mapping>  
    54.     <servlet-name>AxisServlet</servlet-name>  
    55.     <url-pattern>/services/*</url-pattern>  
    56.   </servlet-mapping>  
    57.   
    58.   <servlet-mapping>  
    59.     <servlet-name>SOAPMonitorService</servlet-name>  
    60.     <url-pattern>/SOAPMonitor</url-pattern>  
    61.   </servlet-mapping>  
    62.   
    63.  <!-- uncomment this if you want the admin servlet -->  
    64.  <!--  
    65.   <servlet-mapping>  
    66.     <servlet-name>AdminServlet</servlet-name>  
    67.     <url-pattern>/servlet/AdminServlet</url-pattern>  
    68.   </servlet-mapping>  
    69.  -->  
    70.   
    71.     <session-config>  
    72.         <!-- Default to 5 minute session timeouts -->  
    73.         <session-timeout>5</session-timeout>  
    74.     </session-config>  
    75.   
    76.     <!-- currently the W3C havent settled on a media type for WSDL;  
    77.     http://www.w3.org/TR/2003/WD-wsdl12-20030303/#ietf-draft  
    78.     for now we go with the basic 'it's XML' response -->  
    79.   <mime-mapping>  
    80.     <extension>wsdl</extension>  
    81.      <mime-type>text/xml</mime-type>  
    82.   </mime-mapping>  
    83.     
    84.   
    85.   <mime-mapping>  
    86.     <extension>xsd</extension>  
    87.     <mime-type>text/xml</mime-type>  
    88.   </mime-mapping>  
    89.   
    90.   <welcome-file-list id="WelcomeFileList">  
    91.     <welcome-file>index.jsp</welcome-file>  
    92.     <welcome-file>index.html</welcome-file>  
    93.     <welcome-file>index.jws</welcome-file>  
    94.   </welcome-file-list>  
    95.   
    96. </web-app>  

     

     

    在上面的web.xml中主要是配置了axis的相关配置。

    axis的相关配置

    在上述的web.xml文件里已经对axis进行了配置,可是还须要进行额外的配置。

         复制axis相关的jar文件(官网下载axis-bin-1_4.zip 就有例如以下jar包)

         将axis的相关jar文件拷贝到WEB-INFlib目录下。这些文件包含:

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

      

    測试公布的Web应用

    启动Tomcat服务,打开IE浏览器,訪问地址http://127.0.0.1:8080/WebServiceTest/services,假设看到例如以下界面就说明AXIS部署成功了。

    注意点:本人当时使用的时候 新建的web项目名字为webservice

     


     

    公布WebService

         公布WebService须要使用现有的AdminService来实现。这里我写了一个批处理文件来公布WebService。以后假设须要公布其它文件,仅仅须要改动对应的參数就能够了。

    创建deploy.wsdd文件

    文件deploy.wsdd内容例如以下所看到的:

    Xml代码  收藏代码
    1. <?

      xml version="1.0" encoding="UTF-8"?

      >  

    2. <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">  
    3.     <service name="HelloServices" provider="java:RPC">  
    4.         <parameter name="className" value="com.sinosoft.webservice.HelloService"/>  
    5.         <parameter name="allowedMethods" value="*"/>  
    6.     </service>  
    7. </deployment>  

     

     

    创建公布WebService服务的批处理文件

    批处理文件deploywebservice.bat内容例如以下:

    Xml代码  收藏代码
    1. java -cp axis-ant.jar;axis-schema.jar;axis.jar;commons-discovery-0.2.jar;commons-logging-1.0.4.jar;jaxrpc.jar;log4j-1.2.8.jar;saaj.jar;wsdl4j-1.5.1.jar;xmlsec-1.3.0.jar org.apache.axis.client.AdminClient -lhttp://localhost:8080/WebServiceTest/services/AdminService deploy.wsdd  
    2.   
    3. pause  

     

     

      当中上面的jar包我都拷到和bat文件在同一个文件夹。如今将全部的jar文件都增加到classpath中进行运行。

         -l后的參数是本地要公布WebService的AdminService相应的訪问地址。

         最后deploy.wsdd是相应的配置文件名。

    公布WebService服务

    将deploy.wsdd文件和deploywebservice.bat文件拷贝到同一个文件夹下。运行deploywebservice.bat批处理文件,就能够将deploy.wsdd中描写叙述的Java类公布为WebService。

     项目文件文件夹结构



    公布完毕之后在訪问http://127.0.0.1:8080/WebServiceTest/services例如以下图所看到的:


     

     

    从上图能够看出。公布成功后。多了一个HelloServices的服务。这样就说明HelloService公布成功了。

    查看HelloServices的wsdl

         訪问http://127.0.0.1:8080/WebServiceTest/services/HelloServices?

    wsdl能够看到例如以下wsdl的内容:

    Xml代码  收藏代码
    1. <?

      xml version="1.0" encoding="UTF-8"?>  

    2. <wsdl:definitions targetNamespace="http://127.0.0.1:8080/WebServiceTest/services/HelloServices" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://127.0.0.1:8080/WebServiceTest/services/HelloServices" xmlns:intf="http://127.0.0.1:8080/WebServiceTest/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">  
    3. <!--WSDL created by Apache Axis version: 1.4  
    4. Built on Apr 22, 2006 (06:55:48 PDT)-->  
    5.   
    6.    <wsdl:message name="sayHelloToPersonRequest">  
    7.   
    8.       <wsdl:part name="name" type="soapenc:string"/>  
    9.   
    10.    </wsdl:message>  
    11.   
    12.    <wsdl:message name="sayHelloRequest">  
    13.   
    14.    </wsdl:message>  
    15.   
    16.    <wsdl:message name="sayHelloToPersonResponse">  
    17.   
    18.       <wsdl:part name="sayHelloToPersonReturn" type="soapenc:string"/>  
    19.   
    20.    </wsdl:message>  
    21.   
    22.    <wsdl:message name="sayHelloResponse">  
    23.   
    24.       <wsdl:part name="sayHelloReturn" type="soapenc:string"/>  
    25.   
    26.    </wsdl:message>  
    27.   
    28.    <wsdl:portType name="HelloService">  
    29.   
    30.       <wsdl:operation name="sayHello">  
    31.   
    32.          <wsdl:input message="impl:sayHelloRequest" name="sayHelloRequest"/>  
    33.   
    34.          <wsdl:output message="impl:sayHelloResponse" name="sayHelloResponse"/>  
    35.   
    36.       </wsdl:operation>  
    37.   
    38.       <wsdl:operation name="sayHelloToPerson" parameterOrder="name">  
    39.   
    40.          <wsdl:input message="impl:sayHelloToPersonRequest" name="sayHelloToPersonRequest"/>  
    41.   
    42.          <wsdl:output message="impl:sayHelloToPersonResponse" name="sayHelloToPersonResponse"/>  
    43.   
    44.       </wsdl:operation>  
    45.   
    46.    </wsdl:portType>  
    47.   
    48.    <wsdl:binding name="HelloServicesSoapBinding" type="impl:HelloService">  
    49.   
    50.       <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>  
    51.   
    52.       <wsdl:operation name="sayHello">  
    53.   
    54.          <wsdlsoap:operation soapAction=""/>  
    55.   
    56.          <wsdl:input name="sayHelloRequest">  
    57.   
    58.             <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://webservice.sinosoft.com" use="encoded"/>  
    59.   
    60.          </wsdl:input>  
    61.   
    62.          <wsdl:output name="sayHelloResponse">  
    63.   
    64.             <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://127.0.0.1:8080/WebServiceTest/services/HelloServices" use="encoded"/>  
    65.   
    66.          </wsdl:output>  
    67.   
    68.       </wsdl:operation>  
    69.   
    70.       <wsdl:operation name="sayHelloToPerson">  
    71.   
    72.          <wsdlsoap:operation soapAction=""/>  
    73.   
    74.          <wsdl:input name="sayHelloToPersonRequest">  
    75.   
    76.             <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://webservice.sinosoft.com" use="encoded"/>  
    77.   
    78.          </wsdl:input>  
    79.   
    80.          <wsdl:output name="sayHelloToPersonResponse">  
    81.   
    82.             <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://127.0.0.1:8080/WebServiceTest/services/HelloServices" use="encoded"/>  
    83.   
    84.          </wsdl:output>  
    85.   
    86.       </wsdl:operation>  
    87.   
    88.    </wsdl:binding>  
    89.   
    90.    <wsdl:service name="HelloServiceService">  
    91.   
    92.       <wsdl:port binding="impl:HelloServicesSoapBinding" name="HelloServices">  
    93.   
    94.          <wsdlsoap:address location="http://127.0.0.1:8080/WebServiceTest/services/HelloServices"/>  
    95.   
    96.       </wsdl:port>  
    97.   
    98.    </wsdl:service>  
    99.   
    100. </wsdl:definitions>  

     

    用Java调用WebService实例

         以下是用Java调用刚公布的WebService样例。

     

    Java代码  收藏代码
    1. /* 
    2.  * File name: TestHelloService.java 
    3.  *  
    4.  * Version: v1.0 
    5.  *  
    6.  * Created on Aug 2, 2008 9:54:10 AM 
    7.  *  
    8.  * Designed by Stephen 
    9.  *  
    10.  * (c)Copyright 2008 
    11.  */  
    12. package test.com.sinosoft.webservice;  
    13.   
    14. import java.io.IOException;  
    15. import java.net.MalformedURLException;  
    16.   
    17. import javax.xml.namespace.QName;  
    18. import javax.xml.rpc.ServiceException;  
    19.   
    20. import org.apache.axis.client.Call;  
    21. import org.apache.axis.client.Service;  
    22. import org.apache.commons.logging.Log;  
    23. import org.apache.commons.logging.LogFactory;  
    24.   
    25. /** 
    26.  * @author Stephen 
    27.  *  
    28.  * 測试调用WebService 
    29.  */  
    30. public class TestHelloService {  
    31.     private static final Log log = LogFactory.getLog(TestHelloService.class);  
    32.     private static final String HELLO_SERVICE_ENDPOINT = "http://127.0.0.1:8080/WebServiceTest/services/HelloServices?

      wsdl";  

    33.   
    34.     public static void main(String[] args) {  
    35.         TestHelloService tester = new TestHelloService();  
    36.         // tester.callSayHello();  
    37.         tester.callSayHelloToPerson();  
    38.     }  
    39.   
    40.     public void callSayHello() {  
    41.         try {  
    42.             Service service = new Service();  
    43.             Call call = (Call) service.createCall();  
    44.             call.setTargetEndpointAddress(new java.net.URL(  
    45.                     HELLO_SERVICE_ENDPOINT));  
    46.             //以下名字查询的http://127.0.0.1:8080/WebServiceTest/services/HelloServices?wsdl文件中有  
    47.             call.setOperationName(new QName("http://webservice.sinosoft.com/",  
    48.                     "sayHello"));  
    49.             call.setReturnType(org.apache.axis.Constants.XSD_STRING);  
    50.             try {  
    51.                 //远程调用公布的方法  
    52.                 String ret = (String) call.invoke(new Object[] {});  
    53.                 System.out.println("The return value is:" + ret);  
    54.                 return;  
    55.             } catch (IOException e) {  
    56.                 e.printStackTrace();  
    57.             }  
    58.         } catch (MalformedURLException e) {  
    59.             e.printStackTrace();  
    60.         } catch (ServiceException e) {  
    61.             e.printStackTrace();  
    62.         }  
    63.         log.error("call sayHello service error!");  
    64.     }  
    65.   
    66.     public void callSayHelloToPerson() {  
    67.         try {  
    68.             Service service = new Service();  
    69.             Call call = (Call) service.createCall();  
    70.             call.setTargetEndpointAddress(new java.net.URL(HELLO_SERVICE_ENDPOINT));  
    71.             call.setOperationName(new QName("http://webservice.sinosoft.com/",  
    72.                     "sayHelloToPerson"));  
    73.             call.addParameter("name", org.apache.axis.Constants.XSD_STRING,  
    74.                     javax.xml.rpc.ParameterMode.IN);  
    75.             call.setReturnType(org.apache.axis.Constants.XSD_STRING);  
    76.             try {  
    77.                 String ret = (String) call.invoke(new Object[] { "Stephen" });  
    78.                 System.out.println("The return value is:" + ret);  
    79.                 return;  
    80.             } catch (IOException e) {  
    81.                 e.printStackTrace();  
    82.             }  
    83.         } catch (MalformedURLException e) {  
    84.             e.printStackTrace();  
    85.         } catch (ServiceException e) {  
    86.             e.printStackTrace();  
    87.         }  
    88.         log.error("call sayHello service error!");  
    89.     }  
    90. }  


    传參调用简便方法:
               说明:getSasmOrgUnits  为调用webwervice中的方法的名称  
                //
     private static final String HELLO_SERVICE_ENDPOINT = "http://172.16.100.129:6888/ormrpc/services/WSBaseSasmFacade?wsdl"; 
                Service service = new Service();  
                Call call = (Call) service.createCall();  
                call.setTargetEndpointAddress(new java.net.URL(  
                        HELLO_SERVICE_ENDPOINT));  
                String ret =  (String)call.invoke("getSasmOrgUnits", new Object[] {MD5("參数1"),MD5("參数2")});
                    System.out.println("The return value is:" + ret);  


  • 相关阅读:
    lock free
    Solr 开发环境搭建
    Web中实现网页跳转的方法大总结:
    CSS定位中最难理解的她——absolute的探讨
    JavaScript中正则表达式中遇到的问题——测试匹配
    编写一个Android平台遇到的所有问题(一)——查询sqlite数据库时遇到的问题
    初来乍到,大家好
    在stackoverflow上使用markdown
    提升debian中字体效果
    vim pathogen自动配置
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/6770308.html
Copyright © 2020-2023  润新知