webservice

阅读更多
1.//不用通过wsdl生成类来调用ws,采用发送soap包的方式,动态调用,很方便 
2.import org.apache.cxf.endpoint.Client;    
3.import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;    
4.import org.springframework.context.support.ClassPathXmlApplicationContext;    
5.public class TestMain {    
6. 
7. public static void main(String[] args) {    
8.   String xmlInput = "query              sqlserver1              020";      
9.  JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();    
10.  String wsUrl = "地址"   
11.  String method = "findAllUser"; 
12.  Client client = dcf.createClient(wsUrl);    
13.  Object[] res = null;    
14.  try {    
15.   res = client.invoke(method, xmlInput); 
16.  } catch (Exception e) {    
17.   e.printStackTrace();    
18.  }    
19.    System.exit(0);    
20. }    
21.} 






2,生成客户端代码方式  这种适用cxf
wsimport -keep -d D:/  -p ok http://127.0.0.1/Service.asmx?WSDL



import java.util.List;

import ok.ServiceSoap;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;

import com.ces.xarch.core.web.struts2.StringIDDefineServiceDaoController;
import com.ces.zwww.dao.MacRoomDao;
import com.ces.zwww.entity.MacRoom;
import com.ces.zwww.service.MacRoomService;


public static  List saveMac(){
  //http://127.0.0.1/Service.asmx
  String xml ;
  try {
  JaxWsProxyFactoryBean factory1 = new JaxWsProxyFactoryBean();
  factory1.setServiceClass(ServiceSoap.class); 
  factory1.setAddress("http://127.0.0.1/Service.asmx?wsdl"); 
  ServiceSoap u = (ServiceSoap) factory1.create(); 
  xml= u.getAllMachineRooms();
  System.out.println(xml);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}

 
// return  this.getService().saveMac(xml);
return  null;

  }


用Eclipse生成的如果不是cxf类型是anxi是调用不了的,要用anxi调用

你可能感兴趣的:(webservice)