AXIS1.4安装和应用

/**
* 安装axis
*/
下载:http://ws.apache.org/axis/ 1.4
解压后把webapp/axis目录,放置于%TOMCAT_HOME%\webapps\下。
注意需要另外的lib: activation-1.1.jar, mail-1.4.jar, xercesImpl-2.8.1.jar, xml-apis-2.0.2.jar, xmlsec-1.3.0.jar
设置环境变量:
AXIS_HOME=E:\download\opensource\axis-1_4
AXIS_LIB=%AXIS_HOME%\lib
AXISCLASSPATH=%AXIS_LIB%\axis.jar;%AXIS_LIB%\commons-discovery-0.2.jar;%AXIS_LIB%\commons-logging-1.0.4.jar;%AXIS_LIB%\jaxrpc.jar;%AXIS_LIB%\saaj.jar;%AXIS_LIB%\log4j-1.2.8.jar;%AXIS_LIB%\wsdl4j-1.5.1.jar;%AXIS_LIB%\xml-apis.jar;%AXIS_LIB%\xercesImpl.jar
CLASSPATH=%AXISCLASSPATH%;...

启动tomcat后,访问http://localhost:8080/axis/
查看validator确认安装是否成功。

//部署
java -cp %AXISCLASSPATH% org.apache.axis.client.AdminClient deploy.wsdd
//反部署
java -cp %AXISCLASSPATH% org.apache.axis.client.AdminClient undeploy.wsdd
//测试方法
http://localhost:8080/axis/services/LogTestService?method=testMethod&name=liwanchun
//根据wsdl生成JAVA客户端
Java -Djava.ext.dirs=lib org.apache.axis.wsdl.WSDL2Java http://localhost:8080/axis/services/LogTestService?wsdl


//对于用户的认证
使用BasicHandler实现

//示例:以短信服务为例
/**
* Message.java 2007-12-31  liwanchun Exp $
* Copyright(c) 2000-2007 HC360.COM, All Rights Reserved.
*/
package com.hc360.axis;

/**
* @author: Liwanchun
*/
public class Message {
private String from;
private String to;
private String content;
private int priority;

public String getContent() {
  return content;
}

public void setContent(String content) {
  this.content = content;
}

public String getFrom() {
  return from;
}

public void setFrom(String from) {
  this.from = from;
}   

public int getPriority() {
  return priority;
}

public void setPriority(int priority) {
  this.priority = priority;
}

public String getTo() {
  return to;
}

public void setTo(String to) {
  this.to = to;
}

}

/**
* response.java 2007-12-31  liwanchun Exp $
* Copyright(c) 2000-2007 HC360.COM, All Rights Reserved.
*/
package com.hc360.axis;

/**
* @author: Liwanchun
*/
public class Response {
private int code;

//发送结果代码
private String errorText;
private Message message;

//发送的原始信息 
public int getCode() {
  return code;
}

public void setCode(int code) {
  this.code = code;
}

public String getErrorText() {
  return errorText;
}

public void setErrorText(String errorText) {  
  this.errorText = errorText;
}

public Message getMessage() {
  return message;
}

public void setMessage(Message message) {
  this.message = message;
}

}

/**
* Sms.java 2007-12-31  liwanchun Exp $
* Copyright(c) 2000-2007 HC360.COM, All Rights Reserved.
*/
package com.hc360.axis;

/**
* @author: Liwanchun
*/
public class Sms {
/** 
  * 短信息发送Web服务接口 
  */
 
public Response send(Message msg) throws Exception{
System.out.println("CONTENT:"+msg.getContent());
Response res = new Response();
res.setMessage(msg);
res.setCode(0);
res.setErrorText("ok");
return res;   
  }

}

/**
* 发布Sms
* 1、编写deploy.wsdd
* 2、发布Sms
*/
deploy.wsdd
----------------------------------------------------------------------------------
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
    <service name="Sms" provider="java:RPC">
        <parameter name="className" value="com.hc360.axis.Sms"/>
        <parameter name="allowedMethods" value="send"/>
        <beanMapping qname="myNS:Message" xmlns:myNS="urn:BeanService"  languageSpecificType="java:com.hc360.axis.Message"/>
<beanMapping qname="myNS:Response" xmlns:myNS="urn:BeanService" languageSpecificType="java:com.hc360.axis.Response"/>
    </service>
</deployment>

发布Sms:
----------------------------------------------------------------------------------
java -cp %AXISCLASSPATH% org.apache.axis.client.AdminClient deploy.wsdd


/**
* 根据wsdl生成JAVA客户端
*/
Java -Djava.ext.dirs=lib org.apache.axis.wsdl.WSDL2Java http://localhost:8080/axis/services/LogTestService?wsdl


该命令执行的结果是在当前所在目录下产生一个子目录BeanService和localhost目录,
------------------------------------------------------------------------------------
-- BeanService
  | Message.java
  | Response.java
-- localhost\axis\services\Sms
  | Sms.java 定义了Web服务接口,此例中只有一个send方法。
  | SmsService.java 定义了用于获取Web服务接口的方法。
  | SmsServiceLocator.java 接口SmsService的具体实现
  | SmsSoapBindingStub.java Web服务客户端桩,通过该类与服务器交互。
这四个JAVA类帮我们处理了大部分的逻辑,我们需要的仅仅是把这些类加到我们的项目然后创建一个我们自己的类来调用它们即可。为此我们新加一个类Main.java,为了方便,让这个类与刚产生的四个类都在同一个包下。内容如下:


/**
* Main.java 2007-12-31  liwanchun Exp $
* Copyright(c) 2000-2007 HC360.COM, All Rights Reserved.
*/
package localhost.axis.services.Sms;
import BeanService.Message;
import BeanService.Response;

/**
* @author: Liwanchun
*/
public class Main {
/**
* @param
* @return
* @throws
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
SmsService service = new SmsServiceLocator();
Sms sms = service.getSms();
Message msg = new Message();
msg.setContent("this is a test!");
msg.setFrom("hc360.com");
msg.setTo("yoobaba.com");
msg.setPriority(1);
Response resp = sms.send(msg);

Message msgRec = resp.getMessage();

System.out.println("From:" + msgRec.getFrom() + " to " +
msgRec.getTo() + ", content: " + msgRec.getContent() +
", priority: " + msgRec.getPriority() + ", code: " +
resp.getCode() + ", error code : " + resp.getErrorText());
}catch(Exception e){}

}

}

你可能感兴趣的:(java,apache,tomcat,应用服务器,ext)