原文地址:http://www.blogjava.net/libin2722/archive/2007/11/22/162444.html
今天终于搞定,贴出来分享一下
1、在www.apache.org上下载Axis1.4,将解压后的Axis目录文件复制到%TOMCATHOME%\webapps目录下
2、重新启动Tomcat,访问http://localhost:8080/axis,如果能访问,表示Axis环境安装成功
3、在eclipse下建立webproject,将axis的jar包导入
写2个JavaBean,分别叫做Person.java和Book.java,代码如下
Book.java
package com.lucky.axis.service;
public class Book {
private String bookname;
private Long isbn;
private float price;
private Integer pages;
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public Long getIsbn() {
return isbn;
}
public void setIsbn(Long isbn) {
this.isbn = isbn;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public Integer getPages() {
return pages;
}
public void setPages(Integer pages) {
this.pages = pages;
}
}
Person.java
package com.lucky.axis.service;
import java.util.Map;
public class Person {
private String name;
private int age;
private Map skill;
private Book mybook;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Map getSkill() {
return skill;
}
public void setSkill(Map skill) {
this.skill = skill;
}
public Book getMybook() {
return mybook;
}
public void setMybook(Book mybook) {
this.mybook = mybook;
}
}
建立服务器端代码,命名为PersonInfoService.java,实现三个method,分别为public String getPersonInfo(Person person)、public Book setBookInfo(String bookname, Long isbn)、public Person getPersonInfo(),以下为全部代码:
package com.lucky.axis.service;
import java.util.HashMap;
import java.util.Map;
public class PersonInfoService {
public PersonInfoService() {
}
public String getPersonInfo(Person person) {
String personinfo = "";
personinfo = "Hello, " + person.getName()
+ " ! This is you information\r\n";
personinfo = personinfo + "Ages: " + person.getAge() + "\r\n";
Map skill = person.getSkill();
String strskill = "";
strskill = strskill + skill.get("NAME") + " : you have "
+ skill.get("YEAR") + " years experience!\r\n";
personinfo = personinfo + strskill;
personinfo = personinfo + "Book Name : "
+ person.getMybook().getBookname() + "\r\n";
personinfo = personinfo + "ISBN : "
+ person.getMybook().getIsbn() + "\r\n";
personinfo = personinfo + "You pay $"
+ person.getMybook().getPrice() + " for this book.\r\n";
personinfo = personinfo + "This book have "
+ person.getMybook().getPages() + " pages.\r\n";
return personinfo;
}
public Book setBookInfo(String bookname, Long isbn) {
Book book = new Book();
book.setBookname(bookname);
book.setIsbn(isbn);
book.setPages(new Integer(265));
book.setPrice((float) 25.65);
return book;
}
public Person getPersonInfo() {
Person person = new Person();
person.setName("Aaron");
person.setAge(26);
Map skill = new HashMap();
skill.put("NAME", "JAVA");
skill.put("YEAR", "3");
person.setSkill(skill);
Book book = new Book();
book.setBookname("<Thinking in Java>");
book.setIsbn(new Long(456256821));
book.setPages(new Integer(982));
book.setPrice((float) 99.8);
person.setMybook(book);
return person;
}
}
将编译好的Java类直接放在%Tomcat%\webapps\axis\WEB-INFO\classess\目录下。注意将整个包路径复制到上述目录中
在%Tomcat%\webapps\axis目录下建立 server-config.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">
<globalConfiguration>
<parameter name="sendMultiRefs" value="true" />
<parameter name="disablePrettyXML" value="true" />
<parameter name="adminPassword" value="admin" />
<parameter name="attachments.Directory"
value="C:\Tomcat 5.0 (iplanetldap)\webapps\axis\WEB-INF\attachments" />
<parameter name="axis.servicesPath" value="/mossWS/" />
<parameter name="dotNetSoapEncFix" value="true" />
<parameter name="enableNamespacePrefixOptimization"
value="false" />
<parameter name="sendXMLDeclaration" value="true" />
<parameter name="sendXsiTypes" value="true" />
<parameter name="attachments.implementation"
value="org.apache.axis.attachments.AttachmentsImpl" />
<requestFlow>
<handler type="java:org.apache.axis.handlers.JWSHandler">
<parameter name="scope" value="session" />
</handler>
<handler type="java:org.apache.axis.handlers.JWSHandler">
<parameter name="scope" value="request" />
<parameter name="extension" value=".jwr" />
</handler>
</requestFlow>
</globalConfiguration>
<handler name="Authenticate"
type="java:org.apache.axis.handlers.SimpleAuthenticationHandler" />
<handler name="LocalResponder"
type="java:org.apache.axis.transport.local.LocalResponder" />
<handler name="URLMapper"
type="java:org.apache.axis.handlers.http.URLMapper" />
<handler name="soapmonitor"
type="java:org.apache.axis.handlers.SOAPMonitorHandler">
<parameter name="wsdlURL"
value="/axis/SOAPMonitorService-impl.wsdl" />
<parameter name="namespace"
value="http://tempuri.org/wsdl/2001/12/SOAPMonitorService-impl.wsdl" />
<parameter name="serviceName" value="SOAPMonitorService" />
<parameter name="portName" value="Demo" />
</handler>
<service name="AdminService" provider="java:MSG">
<parameter name="allowedMethods" value="AdminService" />
<parameter name="enableRemoteAdmin" value="false" />
<parameter name="className" value="org.apache.axis.utils.Admin" />
<namespace>http://xml.apache.org/axis/wsdd/</namespace>
</service>
<service name="SOAPMonitorService" provider="java:RPC">
<parameter name="allowedMethods" value="publishMessage" />
<parameter name="className"
value="org.apache.axis.monitor.SOAPMonitorService" />
<parameter name="scope" value="Application" />
</service>
<service name="AxisDemo" provider="java:RPC">
<parameter name="allowedMethod" value="*"></parameter>
<parameter name="className"
value="com.lucky.axis.service.PersonInfoService">
</parameter>
<beanMapping qname="myNS:Person" xmlns:myNS="urn:PersonInfo"
languageSpecificType="java:com.lucky.axis.service.Person" />
<beanMapping qname="myNS:Book" xmlns:myNS="urn:PersonBookInfo"
languageSpecificType="java:com.lucky.axis.service.Book" />
</service>
<transport name="http">
<requestFlow>
<handler type="URLMapper" />
<handler
type="java:org.apache.axis.handlers.http.HTTPAuthHandler" />
</requestFlow>
<parameter name="qs:list"
value="org.apache.axis.transport.http.QSListHandler" />
<parameter name="qs:wsdl"
value="org.apache.axis.transport.http.QSWSDLHandler" />
<parameter name="qs.list"
value="org.apache.axis.transport.http.QSListHandler" />
<parameter name="qs.method"
value="org.apache.axis.transport.http.QSMethodHandler" />
<parameter name="qs:method"
value="org.apache.axis.transport.http.QSMethodHandler" />
<parameter name="qs.wsdl"
value="org.apache.axis.transport.http.QSWSDLHandler" />
</transport>
<transport name="local">
<responseFlow>
<handler type="LocalResponder" />
</responseFlow>
</transport>
</deployment>
其中
<service name="AxisDemo" provider="java:RPC">
<parameter name="allowedMethod" value="*"></parameter>
<parameter name="className"
value="com.lucky.axis.service.PersonInfoService">
</parameter>
<beanMapping qname="myNS:Person" xmlns:myNS="urn:PersonInfo"
languageSpecificType="java:com.lucky.axis.service.Person" />
<beanMapping qname="myNS:Book" xmlns:myNS="urn:PersonBookInfo"
languageSpecificType="java:com.lucky.axis.service.Book" />
</service>
是配置服务端的WebService,name="AxisDemo" 是WebService的名称,使用http://localhost:8080/axis/services/AxisDemo?wsdl来访问,name="allowedMethod" 表示服务类中那些方法需要封装,value="*" 表示所有的方法都封装,如果值封装其中部分方法,需要把方法名称列出来,用“,”逗号分割,name="className" 表示定义WebService服务端的类,value="com.lucky.axis.service.PersonInfoService" 表示将这个类作为WebService类封装,<beanMapping qname="myNS:Person" xmlns:myNS="urn:PersonInfo" languageSpecificType="java:com.lucky.axis.service.Person" />这个表示类中需要引入的其他类
重新启动Tomcat5.5,访问http://localhost:8080/axis/services/AxisDemo?wsdl,可以看到该Web Service的WSDL描述信息
编写客户端测试类,命名为Client.java。在该类中分别对PersonInfoService.java所提供的方法进行调用,代码如下:
package com.lucky.axis.client;
import java.util.HashMap;
import java.util.Map;
import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import org.apache.axis.encoding.ser.BeanSerializerFactory;
import com.lucky.axis.service.Book;
import com.lucky.axis.service.Person;
public class Client {
public Client() {
}
public static void main(String[] args) throws Exception {
// if (args.length < 1) {
// System.out.println("错误:缺少参数");
// System.exit(0);
// }
// Web服务的URL
String url = "http://localhost:8080/axis/services/AxisDemo";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(url);
// 注册序列化/反序列化器
QName qn = new QName("urn:PersonInfo", "Person");
call.registerTypeMapping(Person.class, qn, new BeanSerializerFactory(
Person.class, qn),
new BeanDeserializerFactory(Person.class, qn));
QName qnb = new QName("urn:PersonBookInfo", "Book");
call.registerTypeMapping(Book.class, qnb, new BeanSerializerFactory(
Book.class, qnb), new BeanDeserializerFactory(Book.class, qnb));
// 设置调用方法
call.setOperationName(new javax.xml.namespace.QName("http://test",
"getPersonInfo"));
Person person = new Person();
person.setName("Kiah Han");
person.setAge(26);
Map map = new HashMap();
map.put("NAME", "李彬");
map.put("YEAR", new Integer(3));
person.setSkill(map);
Book book = new Book();
book.setBookname("<Thinking in Java>");
book.setIsbn(new Long(123456789));
book.setPrice((float) 25.98);
book.setPages(new Integer(925));
person.setMybook(book);
// Web服务调用
java.lang.Object _resp = call.invoke(new java.lang.Object[] { person });
// 输出到屏幕
System.out.println(_resp.toString());
System.out
.println("------------Starting setBookInfo()---------------------");
call.setOperationName(new javax.xml.namespace.QName("http://test",
"setBookInfo"));
java.lang.Object _resbook = call.invoke(new java.lang.Object[] {
"<Spring pro>", new Long(987654321) });
Book mybook = (Book) _resbook;
System.out.println(mybook.getBookname());
System.out.println(mybook.getIsbn());
System.out.println(mybook.getPrice());
System.out.println(mybook.getPages());
System.out
.println("------------End setBookInfo()---------------------------");
System.out
.println("------------Starting getPersonInfo()---------------------");
call.setOperationName(new javax.xml.namespace.QName("http://test",
"getPersonInfo"));
java.lang.Object _resperson = call.invoke(new java.lang.Object[] {});
Person aaron = (Person) _resperson;
System.out.println(aaron.getName());
System.out.println(aaron.getAge());
Map skill = aaron.getSkill();
System.out.println(skill.get("NAME"));
System.out.println(skill.get("YEAR"));
Book aaronbook = aaron.getMybook();
System.out.println(aaronbook.getBookname());
System.out.println(aaronbook.getIsbn());
System.out.println(aaronbook.getPrice());
System.out.println(aaronbook.getPages());
System.out
.println("------------End getPersonInfo()---------------------");
}
}
直接运行该Client.java中的main方法,运行结果如下:
Hello, Kiah Han ! This is you information...
Ages: 26
李彬 : you have 3 years experience!
Book Name : <Thinking in Java>
ISBN : 123456789
You pay $25.98 for this book.
This book have 925 pages.
------------Starting setBookInfo()---------------------
<Spring pro>
987654321
25.65
265
------------End setBookInfo()---------------------------
------------Starting getPersonInfo()---------------------
Aaron
26
JAVA
3
<Thinking in Java>
456256821
99.8
982
------------End getPersonInfo()---------------------
成功,好就到这里了,下班了,希望能给大家一点帮助
我的联系方式:
qq:33614814
e-mail:
[email protected]