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

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

接口 com.service.StudentService:

01package com.service;
02  
03import javax.jws.WebParam;
04  
05import javax.jws.WebService;
06  
07@WebService
08  
09public interface StudentService {
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 class StudentServiceImpl implements StudentService{
10  
11    public String 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 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 

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值