利用Java编写简单的WebService实例

利用Java编写简单的WebService实例

      使用Axis编写WebService比较简单,就我的理解,WebService的实现代码和编写Java代码其实没有什么区别,主要是将哪些Java类发布为WebService。下面是一个从编写测试例子到发布WebService,以及编写测试代码的过程介绍。

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

编写服务代码

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

复制代码
package com.sinosoft.webservice;
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服务的例子。

1.在WEB-INF文件夹下创建web.xml文件,该文件的内容如下:

  View Code

在上面的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

     

测试发布的Web应用

     启动Tomcat服务,打开浏览器,访问地址http://localhost:port/项目名/services,如果看到如下界面就说明AXIS部署成功了。

And now... Some Services

  • AdminService (wsdl)
    • AdminService
  • Version (wsdl)
    • getVersion

发布WebService

     发布WebService需要使用现有的AdminService来实现,这里我写了一个批处理文件来发布WebService,以后如果需要发布其他文件,只需要修改相应的参数就可以了。

创建server-config.wsdd文件

     文件server-config.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">
<handler type="java:org.apache.axis.handlers.http.URLMapper" name="URLMapper" />
    <service name="HelloServices" provider="java:RPC">
        <parameter name="className" value="com.sinosoft.webservice.HelloService"/>
        <parameter name="allowedMethods" value="*"/>
    </service>
    <transport name="http">
        <requestFlow>
            <handler type="URLMapper" />
        </requestFlow>
    </transport>
</deployment>
复制代码

最后再次运行Tomcat,访问地址http://localhost:port/项目名/services则出现如下:

And now... Some Services

  • HelloServices (wsdl)
    • sayHello
    • sayHelloToPerson

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

查看HelloServices的wsdl

     直接点击

或者访问http://host:port/ws/services/HelloServices?wsdl可以看到如下wsdl的内容:

复制代码
<wsdl:definitions xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://localhost:8080/TestWeb/services/HelloServices" xmlns:intf="http://localhost:8080/TestWeb/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" targetNamespace="http://localhost:8080/TestWeb/services/HelloServices">
<!--
WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)
-->
<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:message name="sayHelloResponse">
<wsdl:part name="sayHelloReturn" type="soapenc:string"/>
</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:8080/TestWeb/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:8080/TestWeb/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:8080/TestWeb/services/HelloServices"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
复制代码

用Java调用WebService实例

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

复制代码
package 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;

public class TestHelloService {
     private static final Log log = LogFactory.getLog(TestHelloService.class);
        private static final String HELLO_SERVICE_ENDPOINT = "http://localhost:8080/TestWeb/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[] {"zhangkj"});
                    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!");
        }

}
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值