J2EE社区

苟有恒,何必三更起五更眠;
最无益,只怕一日曝十日寒.
posts - 241, comments - 318, trackbacks - 0, articles - 16

java Web Service axis (实例一) server-config.wsdd生成工具

Posted on 2010-08-18 17:20 xcp 阅读(7151) 评论(0)  编辑  收藏 所属分类: SOA
一、第一个实例

(一).服务端
1.服务类(HelloService),加入需要的几个包
package cn.gov.luzhou.SiteServer.user.business.webService;

public class HelloService {
    public String sayHello() {
        return "Hello";
    }

    public String sayHelloToPerson(String name) {
        if (name == null || name.equals("")) {
            name = "nobody";
        }
        return "Hello " + name;
    }
}
2.WEB-INF新建一个deploy.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">
    
<service name="HelloServices" provider="java:RPC">
        
<parameter name="className" value="cn.gov.luzhou.SiteServer.user.business.webService.HelloService"/>
        
<parameter name="allowedMethods" value="*"/>
    
</service>
</deployment>
3.Web Service有3种部署方式(Dynamic Invocation Interface ( DII),Dynamic Proxy方式 ,Stubs方式)在这我们使用Stubs方式

    a.提供一个小工具:deploywebservice.bat为了生成server-config.wsdd

set Axis_Lib=D:\jar
set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib%
%Java_Cmd%  org.apache.axis.client.AdminClient -lhttp://localhost:8080/EGSS_User/services/AdminService deploy.wsdd
  --->说明:d:\jar就是下载下来的axis包的jar路径,解析的时候要用户,将其添加到环境变量;deploy.wsdd就是部署说明文件
     或者直接设置环境变量
 
// 环境变量设置
    A. Client(Windows 2000/NT/XP)
         set AXIS_HOME 
= c:\axis
         set AXIS_LIB  
= %AXIS_HOME%\lib
         set AXISCLASSPATH 
= %AXIS_LIB%\axis.jar;
                             
%AXIS_LIB%\commons-discovery.jar;
                             
%AXIS_LIB%\commons-logging.jar;
                             
%AXIS_LIB%\jaxrpc.jar;%AXIS_LIB%\saaj.jar;
                             
%AXIS_LIB%\log4j-1.2.8.jar;
                             
%AXIS_LIB%\xml-apis.jar;
                             
%AXIS_LIB%\xercesImpl.jar
     B. Server(Unix
+Tomcat4.1.X)
                    set AXIS_HOME
=/usr/axis
         set AXIS_LIB
=$AXIS_HOME/lib
         set AXISCLASSPATH
=$AXIS_LIB/axis.jar:$AXIS_LIB/commons-discovery.jar:
                           $AXIS_LIB
/commons-logging.jar:
                           $AXIS_LIB
/jaxrpc.jar:$AXIS_LIB/saaj.jar:
                           $AXIS_LIB
/log4j-1.2.8.jar:$AXIS_LIB/xml-apis.jar:
                           $AXIS_LIB
/xercesImpl.jar
         export AXIS_HOME;
         export AXIS_LIB; 
         export AXISCLASSPATH
//************ =============================
// deploy service to WEB-INF/server-config.wsdd
java -cp %AXISCLASSPATH% org.apache.axis.client.AdminClient
    
-lhttp://localhost:8080/axistest/services/AdminService 
    src/com/augmentum/roger/axis/svr/deploy.wsdd

   b.运行:deploywebservice.bat
    将会生成一个文件,我就可以通过http://localhost:8080/EGSS_User/services/HelloServices.wsdl请求查看了




(二).客户端
package two;

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;

/**
 * 
 * 测试调用WebService
 
*/

public class TestHelloService {
    
private static final Log log = LogFactory.getLog(TestHelloService.class);
    
private static final String HELLO_SERVICE_ENDPOINT = "http://localhost:8080/EGSS_User/services/HelloServices?wsdl";

    
public static void main(String[] args) {
        TestHelloService tester 
= new TestHelloService();
        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("1.91","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[] "向才鹏" });
                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!");
    }

}

OK了
http://rogershi.blogbus.com/logs/205853.html


名称: ♪4C.ESL | .↗Evon
口号: 遇到新问题♪先要寻找一个方案乄而不是创造一个方案こ
mail: 联系我



只有注册用户登录后才能发表评论。


网站导航: