1.说明:Eclipse版本3.4.2、tomcat版本5.5.12、AXIS2版本1.6.1、 EclipseTomcatPlugin3.2.1准备工作:
下载WAR (Web Archive) Distribution(axis2-1.6.1-war.zip),bin(axis2-1.6.1-bin.zip)
2.下载完后解压至tomcat安装目录下的webapps文件夹下,启动tomcat后,在webapps目录下会生成axis2文件夹。
访问http://localhost:8080/axis2/能 看到以下页面表示axis2运行成功。
3.新建 java? web项目,如项目名为Axis2ServiceDemo,设置编译后的class存入路径为WebContnet/WEB-INF/classes下,在com.dn.service包下建类:HelloFriend,代码如下:
package com.dn.service;
public class HelloFriend {
public String sayHello(String name){
return "hello:"+name;
}
public String saySorry(String name){
return "sorry:"+name;
}
}
4.在WEB-INF下新建web.xml,代码如下:
<?xml version="1.0" encoding="UTF-8"?>?
<web-app id="wmf" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"?
??? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?
??? xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">?
??? <servlet>?
??????? <servlet-name>AxisServlet</servlet-name>?
??????? <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>?
??????? <load-on-startup>1</load-on-startup>?
??? </servlet>?
??? <servlet-mapping>?
??????? <servlet-name>AxisServlet</servlet-name>?
??????? <url-pattern>/services/*</url-pattern>?
??? </servlet-mapping>?
</web-app>?
5.将axis2-1.6.1-war.zip解压后的axis2-web文件夹拷到项目的WebContent下,将conf文件夹,lib文件夹,modules文件夹,services文件夹拷到WebContnet/WEB-INF 下,
6.修改version-1.6.1.arr文件里的services.xml文件,修改后并将原文件替换掉,修改内容如下:
<!--
? ~ Licensed to the Apache Software Foundation (ASF) under one
? ~ or more contributor license agreements. See the NOTICE file
? ~ distributed with this work for additional information
? ~ regarding copyright ownership. The ASF licenses this file
? ~ to you under the Apache License, Version 2.0 (the
? ~ "License"); you may not use this file except in compliance
? ~ with the License. You may obtain a copy of the License at
? ~
? ~ http://www.apache.org/licenses/LICENSE-2.0
? ~
? ~ Unless required by applicable law or agreed to in writing,
? ~ software distributed under the License is distributed on an
? ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
? ~ KIND, either express or implied. See the License for the
? ~ specific language governing permissions and limitations
? ~ under the License.
? -->
<serviceGroup>
<service name="Version">
??? <description>
??????? This service is to get the running Axis version
??? </description>
??? <parameter name="ServiceClass">sample.axisversion.Version</parameter>
??? <operation name="getVersion">
??? <messageReceiver? class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
??? </operation>
</service>
<service name="HelloWorld">
??? <description>
??????? This service is to say HelloFriend
??? </description>
??? <parameter name="ServiceClass">com.dn.service.HelloFriend</parameter>
??? <operation name="sayHello">
??? <messageReceiver? class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
??? </operation>
??? <operation name="saySorry">
??? <messageReceiver? class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
??? </operation>
</service>
</serviceGroup>
多个service放在serviceGroup标签里,<service name="HelloWorld">此处的name设置的service的名称,<parameter name="ServiceClass">设置要发布service的java类,此例是com.dn.service.HelloWorld ,<operation? name="sayHello"> 此处sayHello为提供给外界调用的方法名,<messageReceiver? class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"> 表示有参有回值的方法, <messageReceiver class="org.apache.rpc.receivers.RPCInOnlyMessageReceiver">表示有参没有返回值的方法
7.启动Axis2ServiceDemo后访问http://localhost:8080/Axis2ServiceDemo/services/HelloWorld?wsdl 能看到服务信息 则发布成功
客户端调用webservice
1:解压axis-1.6.1-bin.zip,运行cmd,打开命令窗口,切换到bin目录下,运行 wsdl2java? -uri? http://localhost:8080/Axis2ServiceDemo/services/HelloWorld?wsdl? -p? client? -s? -o? stub? 相关参数设置查看wsdl2java命令设置参数,
在项目下新建com.dn.client包,将生成的客户端拷入该包下
客户端测试代码如下:
HelloWorldStub stub=new HelloWorldStub();
HelloWorldStub.SayHello hello=new HelloWorldStub.SayHello();
hello.setName("洪七");
System.out.println(stub.sayHello(hello).get_return());
System.out.println("----------------------------------");
HelloWorldStub.SaySorry sorry=new HelloWorldStub.SaySorry();
sorry.setName("曾江");
System.out.println(stub.saySorry(sorry).get_return());
输出:hello:洪七
--------------------------
sorry:曾江
方式二调用Webservice:导入jar包:commons-discovery-0.2.jar,javaee.jar
String endpoint="http://localhost:8080/Axis2ServiceDemo/services/HelloWorld?wsdl";
Service service=new Service();
Call call=null;
call=(Call)service.createCall();
//new QName("http://service.dn.com","sayHello"),第一个参数为service的targetNamespace值,第二个参数为要调用的方法名
call.setOperationName(new QName("http://service.dn.com","sayHello"));
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.addParameter(new QName("http://service.dn.com", "name"),org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
call.setUseSOAPAction(true);
call.setSOAPActionURI(endpoint+"sayHello");
String str=(String)call.invoke(new Object[]{"老玩童"});
System.out.println(str);
注:由于文件大小受限,demo若需正常运行需要加入axis2-1.6.1-war.zip解压后的jar包
?