在Axis2环境下发布带包名的POJO

Axis2是一个优秀的web service框架,但是发布POJO的时候很多文章建议:POJO类不能使用package关键字声明

其实,它是可以发布带包名的POJO类的。只不过步骤比较麻烦,大约会有以下10个步骤:


1.首先要建立Axis2的开发环境,本例子使用axis2-1.6.2-bin.zip和axis2-1.6.2-war.zip来建立环境,

开发工具用eclipse+tomcat7

Axis2下载链接:

http://axis.apache.org/axis2/java/core/download.cgi



2.编写和编译POJO类,其实在axis2-1.6.2-bin.zip中有例子源码:


package sample.addressbook.entry;

public class Entry {
    
    private String name = null;
    
    private String street = null;
    
    private String city = null;
    
    private String state = null;
    
    private String postalCode = null;

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPostalCode() {
        return postalCode;
    }

    public void setPostalCode(String postalCode) {
        this.postalCode = postalCode;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }
    
}

================================================

package sample.addressbook.service;


import java.util.HashMap;

import sample.addressbook.entry.Entry;



public class AddressBookService {
    
    private HashMap entries = new HashMap();

    /**
     * Add an Entry to the Address Book
     * @param entry
     */
    public void addEntry(Entry entry) {
        this.entries.put(entry.getName(), entry);
    }
    
    /**
     * Search an address of a person
     *
     * @param name the name of the person whose address needs to be found
     * @return return the address entry of the person.
     */
    public Entry findEntry(String name) {
        return (Entry) this.entries.get(name);
    }
}

3.将编译好的类拷贝到一个发布目录中,注意class的类路径与包名一致。

本例中发布目录为D:\ws,所以类的全路径为:

D:\ws\sample\addressbook\service\AddressBookService.class

D:\ws\sample\addressbook\entry\Entry.class


4.编写配置文件services.xml,

在D:\ws\META-INF\下建立一个services.xml配置文件,内容如下:

<service name="AddressBookService" scope="application">

    <description>
        POJO: AddressBook Service
    </description>

    <messageReceivers>
        
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
                         class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>
        
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
                         class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
    
</messageReceivers>
    
<parameter name="ServiceClass">
sample.addressbook.service.AddressBookService
</parameter>


</service>


其实,在axis2-1.6.2-bin.zip也能找到services.xml配置文件的例子。


5.打包发布文件成aar

使用jar打包工具,打包整个发布目录,注意执行命令的路径:

D:\ws>jar cvf ws.aar .


6.将打包好的文件发布至Axis的service路径下。

本例子是将ws.aar拷贝到

D:\GreenProg\apache-tomcat-7.0.39\webapps\axis2\WEB-INF\services

目录下。


7.重启的Tomat7,访问服务,在浏览器中输入以下路径:

http://localhost:8080/axis2/services/listServices


可以看到发布成功的AddressBookService服务。



8.测试服务,用client程序来测试,测试代码如下:


package sample.addressbook.rpcclient;

import javax.xml.namespace.QName;

import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

import sample.addressbook.entry.Entry;



public class AddressBookRPCClient {

    public static void main(String[] args1) throws AxisFault {

        RPCServiceClient serviceClient = new RPCServiceClient();

        Options options = serviceClient.getOptions();

        EndpointReference targetEPR = new EndpointReference(
                "http://127.0.0.1:8080/axis2/services/AddressBookService");
        options.setTo(targetEPR);

        // /

        /*
         * Creates an Entry and stores it in the AddressBook.
         */

        // QName of the target method
        QName opAddEntry = new QName("http://service.addressbook.sample", "addEntry");

        /*
         * Constructing a new Entry
         */
        Entry entry = new Entry();

        entry.setName("Abby Cadabby");
        entry.setStreet("Sesame Street");
        entry.setCity("Sesame City");
        entry.setState("Sesame State");
        entry.setPostalCode("11111");

        // Constructing the arguments array for the method invocation
        Object[] opAddEntryArgs = new Object[] { entry };

        // Invoking the method
        serviceClient.invokeRobust(opAddEntry, opAddEntryArgs);

       
        
        
        ///
        
        /*
         * Fetching an Entry from the Address book
         */
        
        // QName of the method to invoke
        QName opFindEntry = new QName("http://service.addressbook.sample", "findEntry");

        //
        String name = "Abby Cadabby";

        Object[] opFindEntryArgs = new Object[] { name };
        Class[] returnTypes = new Class[] { Entry.class };

        
        Object[] response = serviceClient.invokeBlocking(opFindEntry,
                opFindEntryArgs, returnTypes);
        
        Entry result = (Entry) response[0];
        
        if (result == null) {
            System.out.println("No entry found for " + name);
            return;
        }
        
        System.out.println("Name   :" + result.getName());
        System.out.println("Street :" + result.getStreet());
        System.out.println("City   :" + result.getCity());
        System.out.println("State  :" + result.getState());
        System.out.println("Postal Code :" + result.getPostalCode());
        

        ///
    }
}

运行测试程序,输出如下结果:

Name   :Abby Cadabby
Street :Sesame Street
City   :Sesame City
State  :Sesame State
Postal Code :11111

9.使用wsdl2java简化客户端访问程序:

wsdl2java命令如下:

D:\axis2-1.6.2-bin\axis2-1.6.2\bin>wsdl2java -uri

http://localhost:8080/axis2/services/AddressBookService?wsdl -p client  -s -o stub


执行完成后,在stub下生成了一个AddressBookServiceStub.java工具类文件,


10。调用stub类文件,访问服务,程序代码如下,注意axis2-1.6.2-bin.zip中的源码有点小BUG:


package sample.addressbook.adbclient;

import client.AddressBookServiceStub;


public class AddressBookADBClient {

    private static String URL = "http://127.0.0.1:8080/axis2/services/AddressBookService";

    public static void main(String[] args) {

        try {
            AddressBookServiceStub stub;

            if (args != null && args.length != 0) {
                stub = new AddressBookServiceStub(args[0]);
                
            } else {
                stub = new AddressBookServiceStub(URL);
            }
            
            AddressBookServiceStub.AddEntry addEntry = new AddressBookServiceStub.AddEntry();
            AddressBookServiceStub.Entry entry = new AddressBookServiceStub.Entry();
            
            entry.setName("Abby Cadabby");
            entry.setStreet("Sesame Street");
            entry.setCity("Sesame City");
            entry.setState("Sesame State");
            entry.setPostalCode("11111");
            
            addEntry.setEntry(entry);
            stub.addEntry(addEntry);
            
            AddressBookServiceStub.FindEntry findEntry = new AddressBookServiceStub.FindEntry();
            
            findEntry.setName("Abby Cadabby");
            
            AddressBookServiceStub.FindEntryResponse response = stub.findEntry(findEntry);
            AddressBookServiceStub.Entry responseEntry = response.get_return();
            
            System.out.println("Name   :" + responseEntry.getName());
            System.out.println("Street :" + responseEntry.getStreet());
            System.out.println("City   :" + responseEntry.getCity());
            System.out.println("State  :" + responseEntry.getState());
            System.out.println("Postal Code :" + responseEntry.getPostalCode());

        } catch (Exception ex) {
            ex.printStackTrace();

        }
    }

}

运行程序,输出如下结果:

Name   :Abby Cadabby
Street :Sesame Street
City   :Sesame City
State  :Sesame State
Postal Code :11111

11.如果要发布多个service,则需要修改配置如下形式,使用<serviceGroup>标记:


<serviceGroup>
  <service name="SpringInit" 
class="sample.spring.service.SpringInit">
    <description>
      This web service initializes Spring.
    </description>
    <parameter name="ServiceClass">
        sample.spring.service.SpringInit
    </parameter>
    <parameter name="ServiceTCCL">composite</parameter>
    <parameter name="load-on-startup">true</parameter>
    <operation name="springInit">
      <messageReceiver 
      class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
    </operation>
  </service>
  <service name="WeatherSpringService">
    <description>
      Weather Spring POJO Axis2 AAR deployment
    </description>
    <parameter name="ServiceClass">
        sample.spring.service.WeatherSpringService
    </parameter>
    <parameter name="ServiceObjectSupplier">
org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier
    </parameter>
    <parameter name="SpringBeanName">
        weatherSpringService
    </parameter>
    <messageReceivers>
      <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
      class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>
      <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
      class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
    </messageReceivers>
  </service>
</serviceGroup>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值