Axis2环境配置和样例测试

 

版本信息:

JDK 1.6

Axis2 1.2 (为什么不用最新版本,因为公司产品线是这个版本)

Tomcat6.0.20

 

1,下载Axis2 1.2,到官网http://axis.apache.org/axis2/java/core/download.cgi下载二进制代码:Binary Distribution zip

2,建立一个web项目,名为webservice。复制所有{AXIS_HOME}/lib包到{TOMCAT_HOME}/lib下。创建web.xml

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <servlet>
    <servlet-name>AxisServlet</servlet-name>
    <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>/servlet/AxisServlet</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>*.jws</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
  </servlet-mapping>
</web-app>

 

2,解压zip文件,打开{AXIS_HOME}/samples/pojoguide, 在这里使用ant命令,完成后在{AXIS_HOME}/samples/pojoguide/build下面找到文件WeatherService.aar,复制WeatherService.aar文件到WEB-INF\services下(也可以部署非打包文件,即将解压后的WeatherService或者自己手工创建的文件目录结构复制到WEB-INF\services。注意:文件包结构必须按照Axis约定,可以参考WeatherService.aar内部结构)。

3,部署web项目到Tomcat(其它web容器也可以)。

4,打开浏览器,在地址栏输入:http://localhost:8080/webservice/services/WeatherService/getWeather ,如果出现下面的内容,则部署成功:

<ns:getWeatherResponse xmlns:ns="http://service.pojo.sample/xsd">
 <ns:return>
  <forecast xmlns="http://data.pojo.sample/xsd">Cloudy with showers</forecast> 
  <howMuchRain xmlns="http://data.pojo.sample/xsd">4.5</howMuchRain> 
  <rain xmlns="http://data.pojo.sample/xsd">true</rain> 
  <temperature xmlns="http://data.pojo.sample/xsd">39.3</temperature> 
 </ns:return>
</ns:getWeatherResponse>

 也可以输入:http://localhost:8080/webservice/services/WeatherService?wsdl 查看wsdl信息

- <wsdl:definitions xmlns:axis2="http://service.pojo.sample" xmlns:ns1="http://service.pojo.sample/xsd" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns0="http://data.pojo.sample/xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://service.pojo.sample">
  <wsdl:documentation>WeatherService</wsdl:documentation> 
- <wsdl:types>
- <xs:schema xmlns:ax21="http://data.pojo.sample/xsd" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://data.pojo.sample/xsd">
  <xs:element name="Weather" type="ax21:Weather" /> 
- <xs:complexType name="Weather">
- <xs:sequence>
  <xs:element name="forecast" nillable="true" type="xs:string" /> 
  <xs:element name="howMuchRain" type="xs:float" /> 
  <xs:element name="rain" type="xs:boolean" /> 
  <xs:element name="temperature" type="xs:float" /> 
  </xs:sequence>
  </xs:complexType>
  </xs:schema>
- <xs:schema xmlns:ns="http://service.pojo.sample/xsd" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://service.pojo.sample/xsd">
- <xs:element name="getWeatherResponse">
- <xs:complexType>
- <xs:sequence>
  <xs:element name="return" nillable="true" type="ns0:Weather" /> 
  </xs:sequence>
  </xs:complexType>
  </xs:element>
- <xs:element name="setWeather">
- <xs:complexType>
- <xs:sequence>
  <xs:element name="weather" nillable="true" type="ns0:Weather" /> 
  </xs:sequence>
  </xs:complexType>
  </xs:element>
  </xs:schema>
  </wsdl:types>
  <wsdl:message name="getWeatherMessage" /> 
- <wsdl:message name="getWeatherResponse">
  <wsdl:part name="part1" element="ns1:getWeatherResponse" /> 
  </wsdl:message>
- <wsdl:message name="setWeatherMessage">
  <wsdl:part name="part1" element="ns1:setWeather" /> 
  </wsdl:message>
- <wsdl:portType name="WeatherServicePortType">
- <wsdl:operation name="getWeather">
  <wsdl:input xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" message="axis2:getWeatherMessage" wsaw:Action="urn:getWeather" /> 
  <wsdl:output message="axis2:getWeatherResponse" /> 
  </wsdl:operation>
- <wsdl:operation name="setWeather">
  <wsdl:input xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" message="axis2:setWeatherMessage" wsaw:Action="urn:setWeather" /> 
  </wsdl:operation>
  </wsdl:portType>
- <wsdl:binding name="WeatherServiceSOAP11Binding" type="axis2:WeatherServicePortType">
  <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> 
- <wsdl:operation name="getWeather">
  <soap:operation soapAction="urn:getWeather" style="document" /> 
- <wsdl:input>
  <soap:body use="literal" /> 
  </wsdl:input>
- <wsdl:output>
  <soap:body use="literal" /> 
  </wsdl:output>
  </wsdl:operation>
- <wsdl:operation name="setWeather">
  <soap:operation soapAction="urn:setWeather" style="document" /> 
- <wsdl:input>
  <soap:body use="literal" /> 
  </wsdl:input>
  </wsdl:operation>
  </wsdl:binding>
- <wsdl:binding name="WeatherServiceSOAP12Binding" type="axis2:WeatherServicePortType">
  <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> 
- <wsdl:operation name="getWeather">
  <soap12:operation soapAction="urn:getWeather" style="document" /> 
- <wsdl:input>
  <soap12:body use="literal" /> 
  </wsdl:input>
- <wsdl:output>
  <soap12:body use="literal" /> 
  </wsdl:output>
  </wsdl:operation>
- <wsdl:operation name="setWeather">
  <soap12:operation soapAction="urn:setWeather" style="document" /> 
- <wsdl:input>
  <soap12:body use="literal" /> 
  </wsdl:input>
  </wsdl:operation>
  </wsdl:binding>
- <wsdl:binding name="WeatherServiceHttpBinding" type="axis2:WeatherServicePortType">
  <http:binding verb="POST" /> 
- <wsdl:operation name="getWeather">
  <http:operation location="getWeather" /> 
- <wsdl:input>
  <mime:content type="text/xml" /> 
  </wsdl:input>
- <wsdl:output>
  <mime:content type="text/xml" /> 
  </wsdl:output>
  </wsdl:operation>
- <wsdl:operation name="setWeather">
  <http:operation location="setWeather" /> 
- <wsdl:input>
  <mime:content type="text/xml" /> 
  </wsdl:input>
  </wsdl:operation>
  </wsdl:binding>
- <wsdl:service name="WeatherService">
- <wsdl:port name="WeatherServiceSOAP11port_http" binding="axis2:WeatherServiceSOAP11Binding">
  <soap:address location="http://192.168.87.222:8080/webservice/services/WeatherService" /> 
  </wsdl:port>
- <wsdl:port name="WeatherServiceSOAP11port_tcp1" binding="axis2:WeatherServiceSOAP11Binding">
  <soap:address location="tcp://192.168.87.222:6061/axis2/services/WeatherService" /> 
  </wsdl:port>
- <wsdl:port name="WeatherServiceSOAP12port_http" binding="axis2:WeatherServiceSOAP12Binding">
  <soap12:address location="http://192.168.87.222:8080/webservice/services/WeatherService" /> 
  </wsdl:port>
- <wsdl:port name="WeatherServiceSOAP12port_tcp1" binding="axis2:WeatherServiceSOAP12Binding">
  <soap12:address location="tcp://192.168.87.222:6061/axis2/services/WeatherService" /> 
  </wsdl:port>
- <wsdl:port name="WeatherServiceHttpport" binding="axis2:WeatherServiceHttpBinding">
  <http:address location="http://192.168.87.222:8080/webservice/services/WeatherService" /> 
  </wsdl:port>
  </wsdl:service>
  </wsdl:definitions>

 到此webservice服务器端已经完成。

 

客户端代码:

1,新建java项目,添加所有{AXIS_HOME}/lib包到classpath。

2,创建java文件WeatherRPCClient.java

package sample.pojo.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.pojo.data.Weather;

public class WeatherRPCClient {

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

        RPCServiceClient serviceClient = new RPCServiceClient();

        Options options = serviceClient.getOptions();

        EndpointReference targetEPR = new EndpointReference("http://localhost:8080/webservice/services/WeatherService");
        options.setTo(targetEPR);

        serviceClient.setOptions(options);
        // Setting the weather
        QName opSetWeather = new QName("http://service.pojo.sample/xsd", "setWeather");

        Weather w = new Weather();

        w.setTemperature((float) 39.3);
        w.setForecast("Cloudy with showers");
        w.setRain(true);
        w.setHowMuchRain((float) 4.5);

        Object[] opSetWeatherArgs = new Object[] {w};

        serviceClient.invokeRobust(opSetWeather, opSetWeatherArgs);

        // Getting the weather
        QName opGetWeather = new QName("http://service.pojo.sample/xsd", "getWeather");

        Object[] opGetWeatherArgs = new Object[] {};
        Class[] returnTypes = new Class[] {Weather.class};

        Object[] response = serviceClient.invokeBlocking(opGetWeather, opGetWeatherArgs, returnTypes);

        Weather result = (Weather) response[0];

        if (result == null) {
            System.out.println("Weather didn't initialize!");
            return;
        }

        // Displaying the result
        System.out.println("Temperature               : " + result.getTemperature());
        System.out.println("Forecast                  : " + result.getForecast());
        System.out.println("Rain                      : " + result.getRain());
        System.out.println("How much rain (in inches) : " + result.getHowMuchRain());

    }
}

3,运行WeatherRPCClient.java,如果出现下面内容,则整个Axis环境搭建完成

Temperature               : 39.3
Forecast                  : Cloudy with showers
Rain                      : true
How much rain (in inches) : 4.5

 

 最少Lib包:

annogen-0.1.0.jar
axiom-api-1.2.4.jar
axiom-impl-1.2.4.jar
axis2-adb-1.2.jar
axis2-java2wsdl-1.2.jar
axis2-kernel-1.2.jar
backport-util-concurrent.jar
commons-codec-1.3.jar
commons-httpclient-3.0.1.jar
commons-logging.jar
neethi-2.0.1.jar
wsdl4j-1.5.1.jar
XmlSchema-1.3.1.jar

  

 

 

你可能感兴趣的:(axis2)