webService發佈和調用--Axis2

 1、工具java

一、下載 Axis2以及eclipse的Axis2插件。http://axis.apache.org/axis2/java/core/download.cgiweb

二、axis2-1.7.1-war.zip解壓,將壓縮包內的axis2.war部署到%TOMCAT-HOME%/webapps下,啓動tomcat,訪問http://localhost:8080/axis2/看是否正常。apache

        點擊Service會進入Service列表頁面,當前只有一個Version服務。http://localhost:8080/axis2/services/Version?wsdltomcat

三、解壓縮eclipse插件 axis2-eclipse-codegen-plugin-1.7.1.zip,axis2-eclipse-service-plugin-1.7.1.zip。解壓後將plugins 複製到%ECLIPSE_HOME%\plugins。app

       

 

2、Axis2發佈Webserviceeclipse

a、新建名稱爲Axis2Service1 的java工程。新建TestWs.javawebapp

       

b.一、打包部署--arr部署方式函數

    有手動打包和插件打包兩種方式,在此我用了插件打包的方式工具

  1. 手動打包
    • 新建\Axis2Service1\deploy文件夾,將\Axis2Service1\bin下的class文件複製過來。
    • 新建\Axis2Service1\deploy\META-INF\services.xml文件

             

<service name="AxisService">
 <description>AxisService</description>
 <parameter name="ServiceClass">ws.TestWs</parameter>
 <operation name="showName">
  <messageReceiver
   class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
 </operation>
 <operation name="getName">
  <messageReceiver
   class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
 </operation>
</service>

      生成aar包 \Axis2Service1\deploy>jar cvf AxisService.aar . (注意帶.號)學習

  2 . 插件打包

    •  IDE中選擇New->other->Axis2 Service Archiver,點擊Next;
    • Class File Location:選擇Axis2Service1\bin目錄,點擊Next;
    • 勾選Skip WSDL,點擊Next;
    • Service Archiver 選擇jar位置,若是沒有jar包就直接點擊Next;
    • 勾選Generate the service xml automatically 自動生成service.xml file文件,點擊Next
    • service name,輸入:AxisService,而後在class name 中填寫要發佈的類(全路徑,如:ws.TestWs),點擊load。勾選 Search declared methods only。點擊next
    • output File location,輸入:D:\ ; output File Name,輸入artiver文件的名稱 AxisService。點擊finish。
    • 提示 Service Archvie generated successfully! 註冊代表,生成成功。

 三、發佈AxisService

      AxisService.aar複製到%TOMCAT-HOME%/webapps/axis2/WEB-INF/services下。(不打aar包,\Axis2Service1\deploy下面複製過去也是能夠)

      打開http://localhost:8080/axis2/services/listServices 就能夠看到剛纔發佈的AxisService服務了,下面有兩個函數:showName,getName。

b.2 獨立部署

  一、新建java web project工程。

  二、文件複製

      %TOMCAT-HOME%\webapps\axis2\WEB-INF\lib 複製到 \Axis2Service2\WebRoot\WEB-INF\lib 下,並加入工程引用。

     %TOMCAT-HOME%\webapps\axis2\WEB-INF\conf 複製到 \Axis2Service2\WebRoot\WEB-INF\conf

     %TOMCAT-HOME%\webapps\axis2\WEB-INF\modules 複製到 \Axis2Service2\WebRoot\WEB-INF\modules

   三、web.xml 代碼以下

  

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="wmf" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <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>/services/*</url-pattern>
 </servlet-mapping>
</web-app>

 二、新建 \Axis2Service2\src\ws\TestWs.java

 

package ws;
public class TestWs {
 public String showName(String name) {return name; }
 public String getName() {return "Axis2Service Sample"; }  
}

 三、新建\Axis2Service2\WebRoot\WEB-INF\services目錄。

 四、新建一個AxisService服務

     AxisService\META-INF\services.xml

   

<service name="AxisService">
 <description>AxisService</description>
 <parameter name="ServiceClass">ws.TestWs</parameter>
 <operation name="showName">
  <messageReceiver
   class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
 </operation>
 <operation name="getName">
  <messageReceiver
   class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
 </operation>
</service>

 啓動tomcat後,訪問http://localhost:8085/Axis2Service2/services/AxisService?wsdl看是否正常。

3、AXIS2調用Web Services

 1、客戶端stub文件生成

     我用的是插件生成方式

 一、腳本生成方式

     去AXIS2的解壓目錄下bin(%AXIS2_HOME%\bin\)下執行下面語句

      wsdl2java -uri http://localhost:8085/Axis2Service2/services/AxisService?wsdl -p ws -s -o stub

   -p參數指定了生成的Java類的包名

   -o參數指定了生成的一系列文件保存的根目錄

   在stub\src\ws自動生成AxisServiceStub.java

二、插件生成方式

     IDE中選擇New->other->Axis2 Code Generator,點擊Next;

    勾選Generate Java source code from a WSDL file,點擊Next;

   WSDL file location,輸入:http://localhost:8085/Axis2Service2/services/AxisService?wsdl,點擊Next;

    若是路徑不對會提示:Specified WSDL is invalid!, Please select a validated *.wsdl/*.xml file on previous page. 正確的話,點擊next;

   指定輸入路徑,點擊Next 提示:All operations completed successfully! 生成成功。

   在D:\src\ws 自動生成了stub一系列文件,其中ws是包名。

     

    上面2種方式生成的stub類有點不同,腳本生成方式是單一文件,插件生成方式生成的一系列文件。

2、客戶端調用 腳本生成方式爲例子,插件生成的相似。

    一、新建 java工程 Axis2Client

          新建\Axis2Client\lib文件夾 將%AXIS2_HOME%\lib\ 下的全部jar包複製到\Axis2Client\lib,並加入工程引用中,加入工程引用的方式以下

     點擊工程右鍵--》Build Path --->Configure Build Paht--->

                

      將經過插件生成在stub文件加入到src\ws下,(如果腳本生成方式,則是單一AxisServiceStub.java文件加入到src\ws下 )

         

      二、新建TestWs.java,在test包下 主要代碼以下

  

//初始化Sub類
AxisServiceStub stub = new AxisServiceStub();
//傳遞AxisServiceStub.ShowName對象,相關參數在這邊賦值。
AxisServiceStub.ShowName command = new AxisServiceStub.ShowName();
command.setName("Hello!");
//取得返回值
String name = stub.showName(command).get_return();
System.out.println(name);

 調用成功後控制檯輸出:Hello!

 

擴展學習---AXIS2調用REST Web Services

   使用http://localhost:8086/Axis2Rest/services/AxisService/showName?name=rest的方式訪問剛纔發佈成功的WebService。從上面能夠看出這個就是rest風格。

   Axis1.0是沒法經過showName?name=rest來獲取信息的。

  二、使用axis客戶端調用

public class TestRest {
 
    private static String toEpr = "http://localhost:8086/Axis2Rest/services/AxisService";
   
    public static void main(String[] args) throws AxisFault {
        Options options = new Options();
        options.setTo(new EndpointReference(toEpr));
       
        //客戶端REST方式調用服務跟普通服務的區別,REST調用必須加上下面這個代碼。
        options.setProperty(Constants.Configuration.ENABLE_REST, Constants.VALUE_TRUE);
        ServiceClient sender = new ServiceClient();
        //axis2-1.5.4不須要下面這句代碼,不然會報錯
        //sender.engageModule(new QName(Constants.MODULE_ADDRESSING));
        sender.setOptions(options);
        OMElement result = sender.sendReceive(getPayload());
        try {
            XMLStreamWriter writer = XMLOutputFactory.newInstance()
                    .createXMLStreamWriter(System.out);
            result.serialize(writer);
            writer.flush();
        } catch (XMLStreamException e) {
            e.printStackTrace();
        } catch (FactoryConfigurationError e) {
            e.printStackTrace();
        }
    }
    private static OMElement getPayload() {
        OMFactory fac = OMAbstractFactory.getOMFactory();
        OMNamespace omNs = fac.createOMNamespace(
                "http://ws", "example1");
        OMElement method = fac.createOMElement("showName", omNs);
        OMElement value = fac.createOMElement("name", omNs);
        value.addChild(fac.createOMText(value, "Rest"));
        method.addChild(value);
        return method;
    }

 

 說明:

       一、sender.engageModule(new QName(Constants.MODULE_ADDRESSING)); axis2-1.5.4不須要下面這句代碼,不然會報錯

       二、客戶端REST方式調用服務跟普通服務的區別,就是Rest有下面這個代碼;

             options.setProperty(Constants.Configuration.ENABLE_REST, Constants.VALUE_TRUE);

         二者返回的數據都是

<ns:showNameResponse xmlns:ns="Resthttp://ws"><ns:return>Rest</ns:return></ns:showNameResponse>

 三、getPayload方法

     OMNamespace omNs = fac.createOMNamespace("http://ws", "example1"); 

    指定命名空間,若是沒對的話會報以下錯誤namespace mismatch require http://ws found http://ws1 OMElement method = fac.createOMElement("showName", omNs);

    要傳遞的方法名爲 "showName" OMElement value = fac.createOMElement("name", omNs);

   傳遞的參數爲name value.addChild(fac.createOMText(value, "Rest"));

    傳遞參數name的值爲Rest。

相關文章
相關標籤/搜索