`
TravelAllRound
  • 浏览: 2164 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

利用Java编写简单的WebService实例

 
阅读更多
      本例子的WebService提供了两个方法,分别是sayHello和sayHelloToPerson,第一个只是返回一个"Hello"字符串,没有参数,第二个函数接受一个字符串作为参数,返回"Hello 参数值",该例子比较简单,但是清楚的说明了从编写代码到发布为WebService以及测试编写好的WebService全过程。
编写服务代码

java代码:
[code="java"]/*
* 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; 
    } 


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

     1. 在myeclipse中创建WebServiceTest的webproject

xml代码:
[code="xml代码"] Apache-Axisorg.apache.axis.transport.http.AxisHTTPSessionListenerAxisServletApache-Axis Servlet 
        org.apache.axis.transport.http.AxisServlet 
    AdminServletAxis Admin Servlet 
        org.apache.axis.transport.http.AdminServlet 
    100SOAPMonitorServiceSOAPMonitorService 
        org.apache.axis.monitor.SOAPMonitorService 
    SOAPMonitorPort5001100AxisServlet/servlet/AxisServletAxisServlet*.jwsAxisServlet/services/*SOAPMonitorService/SOAPMonitor5wsdltext/xmlxsdtext/xmlindex.jspindex.htmlindex.jws 

axis的相关配置
在上述的web.xml文件中已经对axis进行了配置,但是还需要进行额外的配置。

     将axis的相关jar文件复制到WEB-INF\lib文件夹下。这些文件包括:
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应用


发布WebService

xml代码:
[code="xml"] 
创建deploy.wsdd文件

[size=12px;]创建发布WebService服务的批处理文件[/size]



Xml代码

[code="xml"]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 
 
pause 
其中上面的jar包我都拷到和bat文件在同一个目录,现在将所有的jar文件都加入到classpath中进行执行。
     -l后的参数是本地要发布WebService的AdminService对应的访问地址。

发布WebService服务
将deploy.wsdd文件和deploywebservice.bat文件复制到同一个文件夹下,执行deploywebservice.bat批处理文件,就可以将deploy.wsdd中描述的Java类发布为WebService。发布完成之后在访问http://host:port/ws/services如下图所示:

[img]http://images.cnblogs.com/cnblogs_com/liwp_stephen/testwebservices2.JPG" alt="" width="306" height="181" border="0[/img]



从上图可以看出,发布成功后,多了一个HelloServices的服务。这样就说明HelloService发布成功了。
查看HelloServices的wsdl



Xml代码

[code="xml"] 
 
          
 
          
 
          
 
          
 
       

用Java调用WebService实例
     下面是用Java调用刚发布的WebService例子。



Java代码

[code="java"]/*
* 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://127.0.0.1:8080/WebServiceTest/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)); 
            //下面名字查询的http://127.0.0.1:8080/WebServiceTest/services/HelloServices?wsdl文件里有 
            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!"); 
    } 







分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics