axis2发布webService的一个简单实例

来源:互联网 发布:java编程基础 pdf 编辑:程序博客网 时间:2024/05/17 01:21

1.使用eclipse创建包如com\test\service

2.导包,如果缺jar包http://download.csdn.net/detail/b_xinjun1120/4981900

所需包

axis2-codegen-0.94.jar
activation-1.1.jar
axiom-api-1.2.7.jar
axiom-impl-1.2.7.jar
axis2-adb-1.4.1.jar
axis2-kernel-1.1.jar
commons-codec-1.3.jar
commons-httpclient-3.0.1.jar
commons-logging-1.1.1.jar
javax_mail.jar
stax-api-1.0.1.jar
wsdl4j-1.6.2.jar
wstx-asl-3.2.0.jar
XmlSchema-1.4.2.jar

3.创建java文件LoginService后编译

package com.test.service;

import org.apache.axis2.context.MessageContext;
import org.apache.axis2.context.ServiceContext;

public class LoginService {
 public boolean login(String userName, String password) {
  MessageContext context = MessageContext.getCurrentMessageContext();
  ServiceContext ctx = context.getServiceContext();
  if ("user".equals(userName) && "123".equals(password)) {
   ctx.setProperty("userName", userName);
   ctx.setProperty("password", password);
   ctx.setProperty("msg", "success");
   return true;
  }
  ctx.setProperty("msg", "faild");
  return false;
 }
 }

4.进入编译后class的根目录./bin

      创建META-INF目录

      在META-INF中建services.xml

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

<!-- name指定service名称  scope范围application为全局 -->
<service name="wsdlTest" scope="application">
    <!-- 描述 -->

    <description>
       wsdlTest for Test

    </description>

   <!-- 一般情况固定 -->
    <messageReceivers>
        <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
                         class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>
        <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
                         class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
    </messageReceivers>
   <!-- 包名+类名 -->
    <parameter name="ServiceClass">com.test.service.LoginService</parameter>
</service>

4.命令窗口进入编译后class的根目录./bin打aar包

   输入:jar -cvf wsdlTest.aar . (wsdlTest.aar后有一空格和“.”代表当前目录)    

5.http://download.csdn.net/detail/b_xinjun1120/4981900含有axis2-1.5-war.zip减压到Tomcat 6.0\webapps启动Tomcat发布axis2

6..将生成的wsdlTest.aar包放入Tomcat 6.0\webapps\axis2\WEB-INF\services重启tomcat

      查看axis2的发布情况 http://localhost:8080/axis2/services/listServices  点击wsdlTest 到http://localhost:8080/axis2/services/wsdlTest?wsdl 查看wsdl文件

7.可使用soapUI 3.6.1 (http://sourceforge.net/projects/soapui/files/)进行测试

     file-New soapUI Project -Project Name输入一个名称-Initial WSDL/WADLhttp://localhost:8080/axis2/services/wsdlTest?wsdl -ok

     点击Request 1 将两个"?"分别输入user 123 运行(保证tomcat启动)

8.客户端的一个实例

package com.test.service;

import javax.xml.namespace.QName;

import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

public class LoginServiceClient {
 @SuppressWarnings("unchecked")
 public static void main(String[] args) throws AxisFault {
  String target = "http://localhost:8080/axis2/services/LoginService";
   RPCServiceClient client = new RPCServiceClient();
  Options options = client.getOptions();
  options.setManageSession(true);
  
  EndpointReference epr = new EndpointReference(target);
  options.setTo(epr);
  
  QName qname = new QName("http://service.hoo.com", "login");
   Class[] opreturntype = new class[]{string[].class};
  Object[] result = client.invokeBlocking(qname, new Object[] { "admin", "123456" }, opreturntype);
  System.out.println(result[0]);
  qname = new QName("http://service.hoo.com", "getLoginMessage");
  result = client.invokeBlocking(qname, new Object[] { null }, new Class[] { String.class });
  System.out.println(result[0]);
 }
}

查看运行结果与soapui请求的结果对比

    

    

 

 

原创粉丝点击