开发必备
1.apache-cxf-2.2.6
2.spring-ws-1.5.8
3.eclipse-jee-galileo-SR1-win32
开发步骤:
一、新建一个普通的java工程,名字叫WebService_CXF_Client
二、导入apache-cxf-2.2.6 及 spring-ws-1.5.8 下的jar包
三、启动《CXF开发WebService服务器端》所开发的WebService服务器
四、进入apache-cxf-2.2.6\bin文件夹内,运行
wsdl2java.bat http://localhost:8080/WebService_CXF_Host/service/HelloWorld?wsdl
注意,可以查看demo.sprint文件夹内的HelloWorld.java接口,定义的aryMyObjects方法的返回值是List<MyObject>类型,尽管在服务器端定义接口的返回值类型是数组.
五、将整个demo文件夹剪切到WebService_CXF_Client工程的src目录下
六、在src目录下新建一个client-beans.xml文件,内容如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd"> <bean id="client" class="demo.spring.HelloWorld" factory-bean="clientFactory" factory-method="create"/> <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyfactorybean"> <property name="serviceClass" value="demo.spring.HelloWorld"/> <property name="address" value="http://localhost:8080/WebService_CXF_Host/service/HelloWorld"/> </bean> <jaxws:client id="client2" serviceClass="demo.spring.HelloWorld" address="http://localhost:8080/WebService_CXF_Host/service/HelloWorld" /> </beans>
注意:client 与 client2 是两种访问webservice的写法,效果一样。
七、新建含main方法的Client.java类,内容中下:
import java.util.ArrayList; import java.util.List; import org.springframework.context.support.ClasspathXmlApplicationContext; import demo.spring.HelloWorld; import demo.spring.MyObject; public final class Client { private Client() { } public static void main(String args[]) throws Exception { // START SNIPPET: client ClasspathXmlApplicationContext context = new ClasspathXmlApplicationContext(new String[] {"client-beans.xml"}); HelloWorld client = (HelloWorld)context.getBean("client2"); System.out.println("begin stringInt method"); int num=999; String text="i love you"; String response = client.stringInt(text,num); System.out.println("Response: " + response); System.out.println("begin aryMyObjects method"); List<MyObject> lstSource=new ArrayList<MyObject>(); MyObject myObject1=new MyObject(); myObject1.setId(0); myObject1.setName("000"); MyObject myObject2=new MyObject(); myObject2.setId(1); myObject2.setName("111"); lstSource.add(myObject1); lstSource.add(myObject2); List<MyObject> lstResponse=client.aryMyObjects(lstSource); for(int i=0;i<lstResponse.size();i++){ System.out.println("id: "+lstResponse.get(i).getId()+" name: "+lstResponse.get(i).getName()); } System.exit(0); } }
至此,客户端也成功开发了。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。