1. 程式人生 > >CXF動態客戶端呼叫webservice例項

CXF動態客戶端呼叫webservice例項

使用CXF實現WebService,並在客戶端實現動態呼叫編寫伺服器注意事項


注意 :不要指定
@SOAPBinding(style=Style.RPC, use=Use.LITERAL) 因為cxf 不支援:rpc、encoded,在動態客戶呼叫過程。

cxf webservice開發資料,網上一搜大部分是類同的,跟官方的例子一樣。
都是簡單的靜態呼叫例子。對動態呼叫的資料以及錯誤很少。本人被折騰了不行的情況下,
在一哥們的部落格  http://hi.baidu.com/flierssp/item/2de0745c1afc1f3b32e0a96f 中看到動態呼叫不支援RPC才恍然大悟。
伺服器端:
package com.rodjson.cxf;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
/*@SOAPBinding(style=Style.RPC, use=Use.LITERAL) 注意 :如果要動態呼叫一定要註釋掉這句程式碼*/
public interface HelloWorld {
 @WebMethod
 String sayHi(@WebParam(name="text") String text);
 }
package com.rodjson.cxf;
---HelloWorldImpl---
import javax.jws.WebService;

@WebService(endpointInterface="com.rodjson.cxf.HelloWorld",
   
  serviceName="HelloWorld" )
public class HelloWorldImpl implements HelloWorld {
    
           public String sayHi(String text) {
            System.out.println("sayHi called");     
            return "Hello " + text;
          }
          
}

部署:
package com.rodjson.cxf;

import javax.xml.ws.Endpoint;

public class webServiceApp {
 public static void main(String[] args) {
  System.out.println("web service start");
  HelloWorld implementor = new HelloWorldImpl();
  String address = "http://localhost:8080/helloWorld";
  Endpoint.publish(address, implementor);
  System.out.println("web service end");
 }

}
至於部署的方法網上很多,我就不提了,重點是動態客戶端呼叫以及錯誤的識別

package com.rodjson.cxf;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

public class ClientObj {
public static void main(String[] args) {
 JaxWsDynamicClientFactory  factory =JaxWsDynamicClientFactory.newInstance();
    Client client =factory.createClient("http://localhost:8080/cxf/services/HelloWorld?wsdl");
    try {
     Object[] obj =client.invoke("sayHi","xiao");
  System.out.println("resp:"+obj[0]);
 } catch (Exception e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
}
}

錯誤如下:
2012-5-23 20:53:34 org.apache.cxf.common.jaxb.JAXBUtils logGeneratedClassNames

資訊: Created classes: 

javacTask: 無原始檔

用法: javacTask <options> <source files>

-help 用於列出可能的選項

2012-5-23 20:53:34 org.apache.cxf.endpoint.dynamic.DynamicClientFactory createClient