码迷,mamicode.com
首页 > 其他好文 > 详细

【转】Axis1.4开发

时间:2014-11-04 14:36:22      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   color   ar   os   使用   

原文地址: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包导入
bubuko.com,布布扣


2个JavaBean,分别叫做Person.java和Book.java,代码如下
Book.java

bubuko.com,布布扣package com.lucky.axis.service;
bubuko.com,布布扣
bubuko.com,布布扣public class Book {
bubuko.com,布布扣    private String bookname;
bubuko.com,布布扣    private Long isbn;
bubuko.com,布布扣    private float price;
bubuko.com,布布扣    private Integer pages;
bubuko.com,布布扣    public String getBookname() {
bubuko.com,布布扣        return bookname;
bubuko.com,布布扣    }
bubuko.com,布布扣    public void setBookname(String bookname) {
bubuko.com,布布扣        this.bookname = bookname;
bubuko.com,布布扣    }
bubuko.com,布布扣    public Long getIsbn() {
bubuko.com,布布扣        return isbn;
bubuko.com,布布扣    }
bubuko.com,布布扣    public void setIsbn(Long isbn) {
bubuko.com,布布扣        this.isbn = isbn;
bubuko.com,布布扣    }
bubuko.com,布布扣    public float getPrice() {
bubuko.com,布布扣        return price;
bubuko.com,布布扣    }
bubuko.com,布布扣    public void setPrice(float price) {
bubuko.com,布布扣        this.price = price;
bubuko.com,布布扣    }
bubuko.com,布布扣    public Integer getPages() {
bubuko.com,布布扣        return pages;
bubuko.com,布布扣    }
bubuko.com,布布扣    public void setPages(Integer pages) {
bubuko.com,布布扣        this.pages = pages;
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣}
bubuko.com,布布扣



Person.java

bubuko.com,布布扣package com.lucky.axis.service;
bubuko.com,布布扣
bubuko.com,布布扣import java.util.Map;
bubuko.com,布布扣
bubuko.com,布布扣public class Person {
bubuko.com,布布扣    private String name;
bubuko.com,布布扣    private int age;
bubuko.com,布布扣    private Map skill;
bubuko.com,布布扣    private Book mybook;
bubuko.com,布布扣    public String getName() {
bubuko.com,布布扣        return name;
bubuko.com,布布扣    }
bubuko.com,布布扣    public void setName(String name) {
bubuko.com,布布扣        this.name = name;
bubuko.com,布布扣    }
bubuko.com,布布扣    public int getAge() {
bubuko.com,布布扣        return age;
bubuko.com,布布扣    }
bubuko.com,布布扣    public void setAge(int age) {
bubuko.com,布布扣        this.age = age;
bubuko.com,布布扣    }
bubuko.com,布布扣    public Map getSkill() {
bubuko.com,布布扣        return skill;
bubuko.com,布布扣    }
bubuko.com,布布扣    public void setSkill(Map skill) {
bubuko.com,布布扣        this.skill = skill;
bubuko.com,布布扣    }
bubuko.com,布布扣    public Book getMybook() {
bubuko.com,布布扣        return mybook;
bubuko.com,布布扣    }
bubuko.com,布布扣    public void setMybook(Book mybook) {
bubuko.com,布布扣        this.mybook = mybook;
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣}
bubuko.com,布布扣



  建立服务器端代码,命名为PersonInfoService.java,实现三个method,分别为public String getPersonInfo(Person person)、public Book setBookInfo(String bookname, Long isbn)、public Person getPersonInfo(),以下为全部代码:

bubuko.com,布布扣package com.lucky.axis.service;
bubuko.com,布布扣
bubuko.com,布布扣import java.util.HashMap;
bubuko.com,布布扣import java.util.Map;
bubuko.com,布布扣
bubuko.com,布布扣public class PersonInfoService {
bubuko.com,布布扣    public PersonInfoService() {
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣    public String getPersonInfo(Person person) {
bubuko.com,布布扣        String personinfo = "";
bubuko.com,布布扣        personinfo = "Hello, " + person.getName()
bubuko.com,布布扣                + " ! This is you informationbubuko.com,布布扣\r\n";
bubuko.com,布布扣        personinfo = personinfo + "Ages: " + person.getAge() + "\r\n";
bubuko.com,布布扣        Map skill = person.getSkill();
bubuko.com,布布扣        String strskill = "";
bubuko.com,布布扣        strskill = strskill + skill.get("NAME") + " : you have "
bubuko.com,布布扣                + skill.get("YEAR") + " years experience!\r\n";
bubuko.com,布布扣        personinfo = personinfo + strskill;
bubuko.com,布布扣        personinfo = personinfo + "Book Name : "
bubuko.com,布布扣                + person.getMybook().getBookname() + "\r\n";
bubuko.com,布布扣        personinfo = personinfo + "ISBN : "
bubuko.com,布布扣                + person.getMybook().getIsbn() + "\r\n";
bubuko.com,布布扣        personinfo = personinfo + "You pay $"
bubuko.com,布布扣                + person.getMybook().getPrice() + " for this book.\r\n";
bubuko.com,布布扣        personinfo = personinfo + "This book have "
bubuko.com,布布扣                + person.getMybook().getPages() + " pages.\r\n";
bubuko.com,布布扣        return personinfo;
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣    public Book setBookInfo(String bookname, Long isbn) {
bubuko.com,布布扣        Book book = new Book();
bubuko.com,布布扣        book.setBookname(bookname);
bubuko.com,布布扣        book.setIsbn(isbn);
bubuko.com,布布扣        book.setPages(new Integer(265));
bubuko.com,布布扣        book.setPrice((float) 25.65);
bubuko.com,布布扣        return book;
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣    public Person getPersonInfo() {
bubuko.com,布布扣        Person person = new Person();
bubuko.com,布布扣        person.setName("Aaron");
bubuko.com,布布扣        person.setAge(26);
bubuko.com,布布扣        Map skill = new HashMap();
bubuko.com,布布扣        skill.put("NAME", "JAVA");
bubuko.com,布布扣        skill.put("YEAR", "3");
bubuko.com,布布扣        person.setSkill(skill);
bubuko.com,布布扣        Book book = new Book();
bubuko.com,布布扣        book.setBookname("<Thinking in Java>");
bubuko.com,布布扣        book.setIsbn(new Long(456256821));
bubuko.com,布布扣        book.setPages(new Integer(982));
bubuko.com,布布扣        book.setPrice((float) 99.8);
bubuko.com,布布扣        person.setMybook(book);
bubuko.com,布布扣        return person;
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣}
bubuko.com,布布扣


将编译好的Java类直接放在%Tomcat%\webapps\axis\WEB-INFO\classess\目录下。注意将整个包路径复制到上述目录中

bubuko.com,布布扣



在%Tomcat%\webapps\axis目录下建立 server-config.wsdd 文件,内容如下

bubuko.com,布布扣<?xml version="1.0" encoding="UTF-8"?>
bubuko.com,布布扣<deployment xmlns="http://xml.apache.org/axis/wsdd/"
bubuko.com,布布扣    xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
bubuko.com,布布扣    <globalConfiguration>
bubuko.com,布布扣        <parameter name="sendMultiRefs" value="true" />
bubuko.com,布布扣        <parameter name="disablePrettyXML" value="true" />
bubuko.com,布布扣        <parameter name="adminPassword" value="admin" />
bubuko.com,布布扣        <parameter name="attachments.Directory"
bubuko.com,布布扣            value="C:\Tomcat 5.0 (iplanetldap)\webapps\axis\WEB-INF\attachments" />
bubuko.com,布布扣        <parameter name="axis.servicesPath" value="/mossWS/" />
bubuko.com,布布扣        <parameter name="dotNetSoapEncFix" value="true" />
bubuko.com,布布扣        <parameter name="enableNamespacePrefixOptimization"
bubuko.com,布布扣            value="false" />
bubuko.com,布布扣        <parameter name="sendXMLDeclaration" value="true" />
bubuko.com,布布扣        <parameter name="sendXsiTypes" value="true" />
bubuko.com,布布扣        <parameter name="attachments.implementation"
bubuko.com,布布扣            value="org.apache.axis.attachments.AttachmentsImpl" />
bubuko.com,布布扣        <requestFlow>
bubuko.com,布布扣            <handler type="java:org.apache.axis.handlers.JWSHandler">
bubuko.com,布布扣                <parameter name="scope" value="session" />
bubuko.com,布布扣            </handler>
bubuko.com,布布扣            <handler type="java:org.apache.axis.handlers.JWSHandler">
bubuko.com,布布扣                <parameter name="scope" value="request" />
bubuko.com,布布扣                <parameter name="extension" value=".jwr" />
bubuko.com,布布扣            </handler>
bubuko.com,布布扣        </requestFlow>
bubuko.com,布布扣    </globalConfiguration>
bubuko.com,布布扣    <handler name="Authenticate"
bubuko.com,布布扣        type="java:org.apache.axis.handlers.SimpleAuthenticationHandler" />
bubuko.com,布布扣    <handler name="LocalResponder"
bubuko.com,布布扣        type="java:org.apache.axis.transport.local.LocalResponder" />
bubuko.com,布布扣    <handler name="URLMapper"
bubuko.com,布布扣        type="java:org.apache.axis.handlers.http.URLMapper" />
bubuko.com,布布扣    <handler name="soapmonitor"
bubuko.com,布布扣        type="java:org.apache.axis.handlers.SOAPMonitorHandler">
bubuko.com,布布扣        <parameter name="wsdlURL"
bubuko.com,布布扣            value="/axis/SOAPMonitorService-impl.wsdl" />
bubuko.com,布布扣        <parameter name="namespace"
bubuko.com,布布扣            value="http://tempuri.org/wsdl/2001/12/SOAPMonitorService-impl.wsdl" />
bubuko.com,布布扣        <parameter name="serviceName" value="SOAPMonitorService" />
bubuko.com,布布扣        <parameter name="portName" value="Demo" />
bubuko.com,布布扣    </handler>
bubuko.com,布布扣    <service name="AdminService" provider="java:MSG">
bubuko.com,布布扣        <parameter name="allowedMethods" value="AdminService" />
bubuko.com,布布扣        <parameter name="enableRemoteAdmin" value="false" />
bubuko.com,布布扣        <parameter name="className" value="org.apache.axis.utils.Admin" />
bubuko.com,布布扣        <namespace>http://xml.apache.org/axis/wsdd/</namespace>
bubuko.com,布布扣    </service>
bubuko.com,布布扣    <service name="SOAPMonitorService" provider="java:RPC">
bubuko.com,布布扣        <parameter name="allowedMethods" value="publishMessage" />
bubuko.com,布布扣        <parameter name="className"
bubuko.com,布布扣            value="org.apache.axis.monitor.SOAPMonitorService" />
bubuko.com,布布扣        <parameter name="scope" value="Application" />
bubuko.com,布布扣    </service>
bubuko.com,布布扣    <service name="AxisDemo" provider="java:RPC">
bubuko.com,布布扣        <parameter name="allowedMethod" value="*"></parameter>
bubuko.com,布布扣        <parameter name="className"
bubuko.com,布布扣            value="com.lucky.axis.service.PersonInfoService">
bubuko.com,布布扣        </parameter>
bubuko.com,布布扣        <beanMapping qname="myNS:Person" xmlns:myNS="urn:PersonInfo"
bubuko.com,布布扣            languageSpecificType="java:com.lucky.axis.service.Person" />
bubuko.com,布布扣        <beanMapping qname="myNS:Book" xmlns:myNS="urn:PersonBookInfo"
bubuko.com,布布扣            languageSpecificType="java:com.lucky.axis.service.Book" />
bubuko.com,布布扣    </service>
bubuko.com,布布扣    <transport name="http">
bubuko.com,布布扣        <requestFlow>
bubuko.com,布布扣            <handler type="URLMapper" />
bubuko.com,布布扣            <handler
bubuko.com,布布扣                type="java:org.apache.axis.handlers.http.HTTPAuthHandler" />
bubuko.com,布布扣        </requestFlow>
bubuko.com,布布扣        <parameter name="qs:list"
bubuko.com,布布扣            value="org.apache.axis.transport.http.QSListHandler" />
bubuko.com,布布扣        <parameter name="qs:wsdl"
bubuko.com,布布扣            value="org.apache.axis.transport.http.QSWSDLHandler" />
bubuko.com,布布扣        <parameter name="qs.list"
bubuko.com,布布扣            value="org.apache.axis.transport.http.QSListHandler" />
bubuko.com,布布扣        <parameter name="qs.method"
bubuko.com,布布扣            value="org.apache.axis.transport.http.QSMethodHandler" />
bubuko.com,布布扣        <parameter name="qs:method"
bubuko.com,布布扣            value="org.apache.axis.transport.http.QSMethodHandler" />
bubuko.com,布布扣        <parameter name="qs.wsdl"
bubuko.com,布布扣            value="org.apache.axis.transport.http.QSWSDLHandler" />
bubuko.com,布布扣    </transport>
bubuko.com,布布扣    <transport name="local">
bubuko.com,布布扣        <responseFlow>
bubuko.com,布布扣            <handler type="LocalResponder" />
bubuko.com,布布扣        </responseFlow>
bubuko.com,布布扣    </transport>
bubuko.com,布布扣</deployment>


其中

bubuko.com,布布扣<service name="AxisDemo" provider="java:RPC">
bubuko.com,布布扣        <parameter name="allowedMethod" value="*"></parameter>
bubuko.com,布布扣        <parameter name="className"
bubuko.com,布布扣            value="com.lucky.axis.service.PersonInfoService">
bubuko.com,布布扣        </parameter>
bubuko.com,布布扣        <beanMapping qname="myNS:Person" xmlns:myNS="urn:PersonInfo"
bubuko.com,布布扣            languageSpecificType="java:com.lucky.axis.service.Person" />
bubuko.com,布布扣        <beanMapping qname="myNS:Book" xmlns:myNS="urn:PersonBookInfo"
bubuko.com,布布扣            languageSpecificType="java:com.lucky.axis.service.Book" />
bubuko.com,布布扣    </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描述信息
bubuko.com,布布扣


 

编写客户端测试类,命名为Client.java。在该类中分别对PersonInfoService.java所提供的方法进行调用,代码如下:

bubuko.com,布布扣package com.lucky.axis.client;
bubuko.com,布布扣
bubuko.com,布布扣import java.util.HashMap;
bubuko.com,布布扣import java.util.Map;
bubuko.com,布布扣
bubuko.com,布布扣import javax.xml.namespace.QName;
bubuko.com,布布扣
bubuko.com,布布扣import org.apache.axis.client.Call;
bubuko.com,布布扣import org.apache.axis.client.Service;
bubuko.com,布布扣import org.apache.axis.encoding.ser.BeanDeserializerFactory;
bubuko.com,布布扣import org.apache.axis.encoding.ser.BeanSerializerFactory;
bubuko.com,布布扣
bubuko.com,布布扣import com.lucky.axis.service.Book;
bubuko.com,布布扣import com.lucky.axis.service.Person;
bubuko.com,布布扣
bubuko.com,布布扣public class Client {
bubuko.com,布布扣    public Client() {
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣    public static void main(String[] args) throws Exception {
bubuko.com,布布扣//        if (args.length < 1) {
bubuko.com,布布扣//            System.out.println("错误:缺少参数");
bubuko.com,布布扣//            System.exit(0);
bubuko.com,布布扣//        }
bubuko.com,布布扣        // Web服务的URL
bubuko.com,布布扣        String url = "http://localhost:8080/axis/services/AxisDemo";
bubuko.com,布布扣        Service service = new Service();
bubuko.com,布布扣        Call call = (Call) service.createCall();
bubuko.com,布布扣        call.setTargetEndpointAddress(url);
bubuko.com,布布扣
bubuko.com,布布扣        // 注册序列化/反序列化器
bubuko.com,布布扣        QName qn = new QName("urn:PersonInfo", "Person");
bubuko.com,布布扣        call.registerTypeMapping(Person.class, qn, new BeanSerializerFactory(
bubuko.com,布布扣                Person.class, qn),
bubuko.com,布布扣                new BeanDeserializerFactory(Person.class, qn));
bubuko.com,布布扣        QName qnb = new QName("urn:PersonBookInfo", "Book");
bubuko.com,布布扣        call.registerTypeMapping(Book.class, qnb, new BeanSerializerFactory(
bubuko.com,布布扣                Book.class, qnb), new BeanDeserializerFactory(Book.class, qnb));
bubuko.com,布布扣
bubuko.com,布布扣        // 设置调用方法
bubuko.com,布布扣        call.setOperationName(new javax.xml.namespace.QName("http://test",
bubuko.com,布布扣                "getPersonInfo"));
bubuko.com,布布扣        Person person = new Person();
bubuko.com,布布扣        person.setName("Kiah Han");
bubuko.com,布布扣        person.setAge(26);
bubuko.com,布布扣        Map map = new HashMap();
bubuko.com,布布扣        map.put("NAME", "李彬");
bubuko.com,布布扣        map.put("YEAR", new Integer(3));
bubuko.com,布布扣        person.setSkill(map);
bubuko.com,布布扣        Book book = new Book();
bubuko.com,布布扣        book.setBookname("<Thinking in Java>");
bubuko.com,布布扣        book.setIsbn(new Long(123456789));
bubuko.com,布布扣        book.setPrice((float) 25.98);
bubuko.com,布布扣        book.setPages(new Integer(925));
bubuko.com,布布扣        person.setMybook(book);
bubuko.com,布布扣
bubuko.com,布布扣        // Web服务调用
bubuko.com,布布扣        java.lang.Object _resp = call.invoke(new java.lang.Object[] { person });
bubuko.com,布布扣
bubuko.com,布布扣        // 输出到屏幕
bubuko.com,布布扣        System.out.println(_resp.toString());
bubuko.com,布布扣
bubuko.com,布布扣        System.out
bubuko.com,布布扣                .println("------------Starting setBookInfo()---------------------");
bubuko.com,布布扣        call.setOperationName(new javax.xml.namespace.QName("http://test",
bubuko.com,布布扣                "setBookInfo"));
bubuko.com,布布扣        java.lang.Object _resbook = call.invoke(new java.lang.Object[] {
bubuko.com,布布扣                "<Spring pro>", new Long(987654321) });
bubuko.com,布布扣
bubuko.com,布布扣        Book mybook = (Book) _resbook;
bubuko.com,布布扣
bubuko.com,布布扣        System.out.println(mybook.getBookname());
bubuko.com,布布扣        System.out.println(mybook.getIsbn());
bubuko.com,布布扣        System.out.println(mybook.getPrice());
bubuko.com,布布扣        System.out.println(mybook.getPages());
bubuko.com,布布扣        System.out
bubuko.com,布布扣                .println("------------End setBookInfo()---------------------------");
bubuko.com,布布扣
bubuko.com,布布扣        System.out
bubuko.com,布布扣                .println("------------Starting getPersonInfo()---------------------");
bubuko.com,布布扣        call.setOperationName(new javax.xml.namespace.QName("http://test",
bubuko.com,布布扣                "getPersonInfo"));
bubuko.com,布布扣        java.lang.Object _resperson = call.invoke(new java.lang.Object[] {});
bubuko.com,布布扣
bubuko.com,布布扣        Person aaron = (Person) _resperson;
bubuko.com,布布扣        System.out.println(aaron.getName());
bubuko.com,布布扣        System.out.println(aaron.getAge());
bubuko.com,布布扣        Map skill = aaron.getSkill();
bubuko.com,布布扣        System.out.println(skill.get("NAME"));
bubuko.com,布布扣        System.out.println(skill.get("YEAR"));
bubuko.com,布布扣        Book aaronbook = aaron.getMybook();
bubuko.com,布布扣        System.out.println(aaronbook.getBookname());
bubuko.com,布布扣        System.out.println(aaronbook.getIsbn());
bubuko.com,布布扣        System.out.println(aaronbook.getPrice());
bubuko.com,布布扣        System.out.println(aaronbook.getPages());
bubuko.com,布布扣        System.out
bubuko.com,布布扣                .println("------------End getPersonInfo()---------------------");
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣}
bubuko.com,布布扣



   直接运行该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:libin2722@sohu.com

 

原文地址:http://www.blogjava.net/libin2722/archive/2007/11/22/162444.html 

【转】Axis1.4开发

标签:des   style   blog   http   io   color   ar   os   使用   

原文地址:http://www.cnblogs.com/dragon-aslan/p/4073440.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!