广告
返回顶部
首页 > 资讯 > 后端开发 > JAVA >java使用CXF生成客户端实现调用webService接口
  • 342
分享到

java使用CXF生成客户端实现调用webService接口

java开发语言 2023-09-29 05:09:50 342人浏览 安东尼
摘要

一、使用wsimport生成WEBService客户端 wsimport是jdk自带的解析wsdl文件生成本地客户端代码的工具。 生成本地客户端代码首先需要有一个wsdl结尾的访问地址或wsdl文件,

一、使用wsimport生成WEBService客户端

wsimport是jdk自带的解析wsdl文件生成本地客户端代码的工具

生成本地客户端代码首先需要有一个wsdl结尾的访问地址或wsdl文件,如:

Http://localhost:8080/hello/service/hello?wsdl

wsimport的命令常用参数:

命令作用举例
-keep生成java源文件-keep
–encoding指定编码格式-encoding utf-8
-d指定.class文件的输出目录-d 路径
-p定义生成类的包名,不定义的话有默认包名-p 路径
-s指定.java文件的输出目录, 此目录必须存在-s 路径

示例:

wsimport –s . http://localhost/hello?wsdl

-s . .为当前目录

把生成的代码放到项目目录中:

在这里插入图片描述

创建测试类,调用服务:

public class testApp {    public static void main(String[] args) {        DemoWebServiceService demoWebServiceService = new DemoWebServiceService();        DemoWebService demoWebServicePort = demoWebServiceService.getDemoWebServicePort();        String testabs = demoWebServicePort.sayHello("webservice");        System.out.println(testabs);    }}

二、使用CXF生成webService本地客户端

使用cxf框架生成webservice客户端代码 首先需要下载Apache CXF

http://cxf.apache.org/download.html 

下载后配置系统环境变量,在cmd中输入 wsdl2java -h命令测试是否配置成功

在这里插入图片描述

进入下载好的Apache CXF的bin目录下输入cmd命令在黑窗口输入解析wsdl的命令

wsdl2java -d. -p com.xxx.xxx http://localhost:8080/hello/service/hello?wsdl

把解析后生成的客户端代码,放在项目目录下.

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:jaxws="http://cxf.apache.org/jaxws"       xmlns:soap="http://cxf.apache.org/bindings/soap"       xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://cxf.apache.org/bindings/soaphttp://cxf.apache.org/schemas/configuration/soap.xsdhttp://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd">    <!-- 引入CXF Bean定义如下,早期的版本中使用 -->    <import resource="classpath:META-INF/cxf/cxf.xml" />    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />    <!-- 注册cxf客户端的代理对象,通过spring创建代理对象,通过代理对象调用远程服务 -->    <jaxws:client id="myClient" address="http://localhost:8080/hello/service/hello" serviceClass="com.T1aN.cxfClient.HelloService"></jaxws:client></beans>

测试:

public class TestApp {    public static void main(String[] args) {        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("cxf.xml");        HelloService myClient = (HelloService) classPathXmlApplicationContext.getBean("myClient");        String cxf = myClient.sayHello("cxf");        System.out.println(cxf);    }}

来源地址:https://blog.csdn.net/T1aN_/article/details/129875335

--结束END--

本文标题: java使用CXF生成客户端实现调用webService接口

本文链接: https://www.lsjlt.com/news/421108.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作