加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 00:18:15 所属栏目:安全 来源:网络整理
导读:服务接口及实现类很简单 : 接口? 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 ????

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

接口?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服务!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读