一、生成实现:如:
wsdl2java -client -d e:\src -p com.client -impl [ws地址]
生成后把相关实现复制/导入到测试工程
二、建立XML环境配置,如client.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:client id="online" address="http://localhost:8880/test/hello"
serviceClass="com.client.test" />
</beans>
ID为标识,address为WS的地址,serviceClass为具体的类(针对于生成的实现)
三、在main方法中进行调用测试
方法一:使用上面的配置文件:
ApplicationContext context = new ClassPathXmlApplicationContext("com/client/client.xml"); //加载WS的XML环境配置
test service = (test)context.getBean("online"); //从环境配置中实例化WS类
String message=FileUtils.readFileToString(new File("E:\\ATCO\\msg.xml"),"GBK"); //从文件加载参数,如需要传入XML
String datatype="corpdata.regist"; //另一个参数
String result = service.realtimeQuery(message, datatype); //调用方法,传入参数,获取返回结果
System.out.println(result); //输出结果
方法二:生成实现后直接调用,WS地址直接指定:
String wsdl ="http://192.168.1.168:8001/test/hello?wsdl";
URL url = new URL(wsdl);
testImplService s = new testImplService(url, testImplService.SERVICE);
test service = s.gettestImplPort();
String message=FileUtils.readFileToString(new File("E:\\ATCO\\msg.xml"),"GBK");
String datatype="corpdata.regist";
String retString=service.realtimeQuery(message, datatype);
System.out.println(retString);