上一节中已经成功的发布了一个服务端webservice,这一部分将默认客户端调用webservice
这里我们重新建立了一个新的工程CXF-Client,把上一节中列举的jar包都搞过来即可,调用方式采用了和RMI类似的机制,即客户端直接服务器端提供的服务接口(interface),CXF通过运行时代理生成远程服务的代理对象,在客户端完成对webservice的访问
几个必填的字段:setAddress-这个就是我们发布webservice时候的地址,保持一致
HelloWorldService:需要服务器端提供单独的Jar文件过来
1. package com.crazycoder2010.webservice.cxf.client;
2.
3. import org.apache.cxf.interceptor.LoggingInInterceptor;
4. import org.apache.cxf.interceptor.LoggingOutInterceptor;
5. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
6.
7. import com.crazycoder2010.webservice.cxf.server.HelloWorldService;
8.
9. public class Client {
10. public static void main(String[] args) {
11. new JaxWsProxyFactoryBean();
12. new LoggingInInterceptor());
13. new LoggingOutInterceptor());
14. class);
15. "http://localhost:9090/helloWorldService");
16. HelloWorldService helloWorldService = (HelloWorldService)bean.create();
17. "Kevin");
18. System.out.println(result);
19. }
20. }
运行输出:
1. 2011-8-9 21:32:20 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
2. 信息: Creating Service {http://server.cxf.webservice.crazycoder2010.com/}HelloWorldServiceService from class com.crazycoder2010.webservice.cxf.server.HelloWorldService
3. 2011-8-9 21:32:41 org.apache.cxf.interceptor.AbstractLoggingInterceptor log
4. 信息: Inbound Message
5. ----------------------------
6. ID: 1
7. Response-Code: 200
8. Encoding: UTF-8
9. Content-Type: text/xml;charset="utf-8"
10. Headers: {Content-type=[text/xml;charset="utf-8"], Transfer-encoding=[chunked]}
11. Payload: <?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:sayHelloResponse xmlns:ns2="http://server.cxf.webservice.crazycoder2010.com/"><return>Hello Kevin</return></ns2:sayHelloResponse></S:Body></S:Envelope>
12. --------------------------------------
13. Hello Kevin
查看了官方的文档后,发现其实是可以实现不依赖服务器端接口来完成调用的,如下
使用JaxWsDynamicClientFactory类,只要指定服务器端wsdl文件的位置,然后指定要调用的方法和方法的参数即可,so simple!!
1. package com.crazycoder2010.webservice.cxf.client;
2.
3. import org.apache.cxf.endpoint.Client;
4. import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
5.
6. public class Client3 {
7. public static void main(String[] args) throws Exception {
8. JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();
9. "http://localhost:9090/helloWorldService?wsdl");
10. "sayHello", "KEVIN");
11. 0]);
12. }
13. }
标签:webservice,CXF,HelloWorld,HelloWorldService,cxf,import,apache,org,客户端
From: https://blog.51cto.com/u_809530/8274900