« 体验纯Java数据库——Derby | Main | 开源servlet容器——Jetty »
http://developers.sun.com.cn/blog/functionalca/date/20090514 星期四 2009年05月14日

Axis简介

作者:曹祺
Blog: http://blogs.sun.com/greysh
Web: http://www.greysh.com
Email: Qi.Cao@Sun.com
本文链接:
http://developers.sun.com.cn/blog/functionalca/entry/axis%E7%AE%80%E4%BB%8B 
源代码下载:
http://developers.sun.com.cn/blog/functionalca/resource/Greysh/FCA_Greysh_WS.zip  
难度:入门

Axis是apache基金会的web service的一个子项目,可以写jws,也可以自己写wsdd,本文以jws方式发布.WebService可以通过XML和其他系统交互,而以前的EJB只能用Java,虽然后来有了CORBA,但是作为将来的趋势,应该还是Web Service

需要用的基础jar有

  activation.jar
  axis-ant.jar
  axis.jar
  commons-discovery-0.2.jar
  commons-logging-1.0.4.jar
  jaxrpc.jar
  log4j-1.2.8.jar
  mail.jar
  saaj.jar
  wsdl4j-1.5.1.jar
  xmlsec-1.4.2.jar
然后配置web.xml,可以用axis的默认配置

 

然后写一个服务端的jws文件

public class Server {
    public String invoke(String message){
        return "The server invokes:" + message;
    }
}
然后写一个客户端去调用

package com.greysh.service;

import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;

import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

public class TestClient {

 public static void main(String[] args) throws ServiceException,
   MalformedURLException, RemoteException {
  String endpoint = "http://localhost:8080/FCA_Greysh_WebService/Server.jws?wsdl";
  Service service = new Service();
  Call call = (Call) service.createCall();
  call.setTargetEndpointAddress(new URL(endpoint));
  call.setOperationName(new QName(
    "http://localhost:8080/FCA_Greysh_WebService", "invoke"));
  String result = (String) call.invoke(new Object[] { "Test!" });
  System.out.println(result);
 }
}

运行的流程是先找到WSDL,然后实例化service,再根据函数名和URI去调用方法,参数通过一个Object数组传入

启动tomcat后会在WEB-INF生成attachments

运行TestClient,如果配置成功,运行时候会根据WSDL去调用

控制台会输出The server invokes:Test!

axis会自动生成wsdl

http://localhost:8080/FCA_Greysh_WebService/Server.jws?wsdl



发表于 Sun Functional 校园大使 [JavaEE] ( 五月 14, 2009 10:16 下午 ) Permalink | 评论[0]
评论:

发表一条评论:
  • HTML语法: 禁用