使用JaxWsDynamicClientFactory动态调用自己创建的WebService服务

服务接口及实现类很简单:

接口 com.service.StudentService:

01 package com.service;
02   
03 import javax.jws.WebParam;
04   
05 import javax.jws.WebService;
06   
07 @WebService
08   
09 public interface StudentService {
10   
11     String helloStudent(@WebParam(name="text")String name);
12   
13 }

实现类 com.service.impl.StudentServiceImpl:

01 package com.service.impl;
02   
03 import javax.jws.WebService;
04   
05 import com.service.StudentService;
06   
07 @WebService(endpointInterface="com.service.StudentService", targetNamespace="http://service.com/")
08   
09 public class StudentServiceImpl implements StudentService{
10   
11     public String helloStudent(String name) {
12   
13         return "hello " + name;
14   
15     }
16   
17 }

server: com.jettyServer.ServerForJetty:

01 package com.jettyServer;
02   
03 import javax.xml.ws.Endpoint;
04   
05 import com.service.impl.StudentServiceImpl;
06   
07 public class ServerForJetty {
08   
09     public static void main(String[] args) throws InterruptedException {
10   
11         StudentServiceImpl implementor = new StudentServiceImpl();
12   
13         String address = "http://localhost:9000/student";
14   
15         Endpoint.publish(address, implementor);
16   
17     }
18   
19 }

client:com.client.Test 

01 package com.client;
02   
03 import org.apache.cxf.endpoint.Client;
04   
05 import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
06   
07 public class Test {
08   
09     public static void main(String[] args) throws Exception {
10   
11         JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
12   
13          
1         Client client = dcf.createClient("http://localhost:9000/student?wsdl");
2  
3         Object[] res = client.invoke("helloStudent", "LeeThinker");
4  
5         System.out.println("Echo response: " + res[0]);
6   
7     }
8   
9 }

先启动JettyServer, 后访问http://localhost:9000/student?wsdl成功刷新出wsdl, Ok, 服务顺利启动!

再执行Testmain方法, Congratulations! 这次真正使用Dynamic的方式在不需要通过wsdl2java生成客户端java类文件的情况下成功调用WebService服务!

你可能感兴趣的:(使用JaxWsDynamicClientFactory动态调用自己创建的WebService服务)