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

来源:互联网 发布:淘宝黑搜索 编辑:程序博客网 时间:2024/06/05 08:31

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

接口 com.service.StudentService:

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

实现类 com.service.impl.StudentServiceImpl:

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

server: com.jettyServer.ServerForJetty:

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

client:com.client.Test 

01package com.client;
02  
03import org.apache.cxf.endpoint.Client;
04  
05import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
06  
07public classTest {
08  
09    publicstaticvoid 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服务!

0 0
原创粉丝点击