axis2開發webservice程序

1、環境java

  eclipse + jdk 6.0 + win7 64位 +tomcat7.0web

2、建立服務端程序apache

  一、新建web項目,webserviceTesttomcat

  二、下載axis2,將lib目錄下的jar包複製到web項目lib目錄下eclipse

  三、建立服務端程序HelloWorld.javaide

  

1 package com.bwy.ws;
2 
3 public class HelloWorld {
4     public String sayHello(String name) {
5         String sayResult = "Hello," + name;
6         System.out.println(sayResult);
7         return sayResult;
8     }
9 }

  四、配置web容器,tomcat測試

  五、發佈服務端程序,啓動tomcaturl

3、發佈webservice服務spa

    右擊HelloWorld.java  --> Web Service --> Creat web service  一直點擊next 直到 finish插件

  生成後的工程以下所示:

4、測試服務

  右擊HelloWorld.wsdl  --> web service --> test with web service exploer

  選擇方法名,輸入參數,點擊GO,如圖所示,表示測試成功:

  

   注:這裏若是不先啓動tomcat發佈web服務,會報錯:IWAB0135E An unexpected error has occurred

5、編寫客戶端代碼調用webservice

  新建工程webServiceClient,新建類HelloWorldTest,

  

 1 package com.bwy.client;
 2 
 3 import java.rmi.RemoteException;
 4 
 5 import javax.xml.rpc.ParameterMode;
 6 import javax.xml.rpc.ServiceException;
 7 
 8 import org.apache.axis.client.Call;
 9 import org.apache.axis.client.Service;
10 import org.apache.axis.encoding.XMLType;
11 
12 public class HelloWorldTest {
13     
14     public static void main(String[] args) {
15         HelloWorldTest test = new HelloWorldTest();
16         String result = test.invokeService();
17         System.out.println(result);
18     }
19     
20     
21     /**
22      * 調用webservice服務
23      * 
24      * @return 服務信息
25      */
26     public String invokeService() {
27 
28         // 遠程調用路徑
29         String url = "http://localhost:8080/WebServiceTest/services/HelloWorld";
30         
31         //返回值
32         String result = "";
33         Service serive = new Service();
34         Call call = null;
35         try {
36             call = (Call) serive.createCall();
37             call.setTargetEndpointAddress(url);
38 
39             // 調用的方法名
40             call.setOperationName("sayHello");
41 
42             // 設置參數名
43             call.addParameter("name", // 參數名
44                     XMLType.XSD_STRING, // 參數類型:String
45                     ParameterMode.IN); // 參數模式:'IN' or 'OUT'
46             
47             //設置返回值類型
48             call.setReturnTypeAsHeader(XMLType.XSD_STRING);
49             String name = "wayne";
50             
51             //遠程調用
52             result = (String) call.invoke(new Object[]{name});
53         } catch (ServiceException e) {
54             e.printStackTrace();
55         } catch (RemoteException e) {
56             e.printStackTrace();
57         }
58         return result;
59     }
60 }
View Code

 

  右擊運行:

  執行的時候,發現能訪問服務端程序,但接收服務端返回的信息時報錯:
  AxisFault
 faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server.userException
 faultSubcode:
 faultString: org.xml.sax.SAXException: Found instance data for {http://ws.bwy.com}sayHelloReturn in the soap:body instead of the soap:header.
 faultActor:
 faultNode:
 faultDetail:
    {http://xml.apache.org/axis/}stackTrace:org.xml.sax.SAXException: Found instance data for {http://ws.bwy.com}sayHelloReturn in the soap:body instead of the soap:header.
    at org.apache.axis.message.RPCHandler.onStartChild(RPCHandler.java:209)
    at org.apache.axis.encoding.DeserializationContext.startElement(DeserializationContext.java:1035)
    at org.apache.axis.message.SAX2EventRecorder.replay(SAX2EventRecorder.java:165)
    at org.apache.axis.message.MessageElement.publishToHandler(MessageElement.java:1141)
    at org.apache.axis.message.RPCElement.deserialize(RPCElement.java:236)
    at org.apache.axis.message.RPCElement.getParams(RPCElement.java:384)
    at org.apache.axis.client.Call.invoke(Call.java:2467)
    at org.apache.axis.client.Call.invoke(Call.java:2366)
    at org.apache.axis.client.Call.invoke(Call.java:1812)
    at com.bwy.client.HelloWorldTest.invokeService(HelloWorldTest.java:52)
    at com.bwy.client.HelloWorldTest.main(HelloWorldTest.java:16)

    {http://xml.apache.org/axis/}hostname:bwy

org.xml.sax.SAXException: Found instance data for {http://ws.bwy.com}sayHelloReturn in the soap:body instead of the soap:header.
    at org.apache.axis.AxisFault.makeFault(AxisFault.java:101)
    at org.apache.axis.client.Call.invoke(Call.java:2470)
    at org.apache.axis.client.Call.invoke(Call.java:2366)
    at org.apache.axis.client.Call.invoke(Call.java:1812)
    at com.bwy.client.HelloWorldTest.invokeService(HelloWorldTest.java:52)
    at com.bwy.client.HelloWorldTest.main(HelloWorldTest.java:16)
Caused by: org.xml.sax.SAXException: Found instance data for {http://ws.bwy.com}sayHelloReturn in the soap:body instead of the soap:header.
    at org.apache.axis.message.RPCHandler.onStartChild(RPCHandler.java:209)
    at org.apache.axis.encoding.DeserializationContext.startElement(DeserializationContext.java:1035)
    at org.apache.axis.message.SAX2EventRecorder.replay(SAX2EventRecorder.java:165)
    at org.apache.axis.message.MessageElement.publishToHandler(MessageElement.java:1141)
    at org.apache.axis.message.RPCElement.deserialize(RPCElement.java:236)
    at org.apache.axis.message.RPCElement.getParams(RPCElement.java:384)
    at org.apache.axis.client.Call.invoke(Call.java:2467)
    ... 4 more

 後更改以下代碼解決:

  

// 設置返回值類型
            // call.setReturnTypeAsHeader(XMLType.XSD_STRING);
            call.setReturnType(XMLType.XSD_STRING);

  這個什麼緣由還未搞懂,看到此文的朋友若是有了解的能夠給我留言,感謝~~

 

6、生成客戶端代碼

  一、經過命令窗口手動生存客戶端代碼

    客戶端輸入以下命令:

  C:\Users\bwy>wsimport -s F:\MYWORK\webservice\WebServiceClient\src -p com.bwy.client2 -keep http://localhost:8080/WebServiceTest/services/HelloWorld?wsdl

    刷新工程,以下所示:

    

    建立測試類,代碼以下:

    

 1 package com.bwy.client2;
 2 
 3 public class ClientTest {
 4     
 5     public static void main(String[] args) {
 6         HelloWorld hw = new HelloWorldService().getHelloWorld();
 7         String result = hw.sayHello("zhangsan");
 8         System.out.println("the response is:"+result);
 9     }
10 }
View Code

 

    二、經過eclipse客戶端插件自動生成客戶端代碼

 

         將服務端工程生成的wsdl文件放入客戶端工程下,右擊-->webservice -->generate client 

      

 

至此,axis2簡單webservice實例完成。

相關文章
相關標籤/搜索