第一种静态调用web service的方法
try {
//创建webservice命名空间
javax.xml.namespace.QName SERVICE_NAME = new QName("http://tempuri.org/", "cc2erp");
//Cc2Erp,Cc2ErpSoap是用cxf框架生成的实例
Cc2Erp ccErp = new Cc2Erp(new URL(Constants.CON_SERVICE_URL),SERVICE_NAME);
Cc2ErpSoap port = ccErp.getCc2ErpSoap();
//tok是用来发送请求的用户名和密码
String tok = port.getToken("test","fesco");
//用来获取需要调用的json串
String Json = 需要获取的数据,这个方法中将数据转为json串传递;
//将tok和json串传给web service
createResult = port.createOutbandTask(tok, taskInstJson);
log.fine("CreateOutbandTask : "+createResult);
} catch (Exception e) {
e.printStackTrace();
createResult = false;
log.fine(e);
}
第二种通过http方式动态调用web
service的方法
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.ConnectionType;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
public class Utils {
//动态调用wsdl客户端工厂
private static final JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
//设置最大连接时间
private static final long CONNECTION_TIME = 30000;
//设置关闭
private static final ConnectionType CONNECTION_TYPE = ConnectionType.CLOSE;
//创建客户端
private static Client getClient(String url){
return dcf.createClient(url);
}
//创建通过http连接web service
public static Object[] invoke(String url,String methodName,Object... params) throws Exception{
Client client = getClient(url);
//设置HTTP连接管道
HTTPConduit http = (HTTPConduit) client.getConduit();
//设置HTTP连接政策
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
httpClientPolicy.setConnectionTimeout(CONNECTION_TIME);
httpClientPolicy.setConnection(CONNECTION_TYPE);
http.setClient(httpClientPolicy);
//返回调用的方法名和参数
return client.invoke(methodName, params);
}
public static void main( String[] args )
{
System.out.println(invoke("http://192.168.191.1:9001/fws-outbound-service/webservice/OutboundService?wsdl",
"getToken","fesco", "d31fae18a821e71fc004044d00ef4033")[0]);
}
}
第三种通过cxf动态调用web
service的方法
/**
* 通过cxf框架动态调用web service
*/
public static void main(String args) throws Exception {
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://localhost:9001/.../**?wsdl"); //创建客户端
//client.
Object[] res = client.invoke("getToken", "username", "userpwd");
System.out.println("------------------");
System.out.println( res[0] );
System.out.println("------------------");
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
factory.setServiceClass(HelloWorld.class);
factory.setAddress("http://localhost:9001/HelloWorld");
HelloWorld clientHelloWorld = (HelloWorld) factory.create();
String reply = clientHelloWorld.sayHi("HI");
System.out.println("------------------");
System.out.println(reply);
System.out.println("------------------");
}