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

一、使用wsimport生成webService客户端

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

1.生成本地客户端代码首先需要有一个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本地客户端

1.使用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/beans
					http://www.springframework.org/schema/beans/spring-beans.xsd
					http://cxf.apache.org/bindings/soap
					http://cxf.apache.org/schemas/configuration/soap.xsd
					http://cxf.apache.org/jaxws
					http://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);
    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Apache CXF是一个开源的WebService框架,可以帮助用户快速、简便地开发和部署WebService应用程序。它提供了多种方式来调用WebService接口,下面介绍几种常用的方式: 1. 使用JAX-WS API:CXF实现了JAX-WS API,可以直接使用JAX-WS提供的API来调用WebService。示例代码如下: ```java HelloWorldService service = new HelloWorldService(); HelloWorld port = service.getHelloWorldPort(); String result = port.sayHello("CXF"); ``` 2. 使用代理方式:CXF可以根据WebService WSDL文件自动生成代理类,然后通过调用代理类的方法来调用WebService接口。示例代码如下: ```java JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setServiceClass(HelloWorld.class); factory.setAddress("http://localhost:8080/HelloWorld"); HelloWorld client = (HelloWorld) factory.create(); String result = client.sayHello("CXF"); ``` 3. 使用Spring配置文件:CXF提供了Spring配置文件方式来实现WebService接口调用。用户可以在Spring配置文件中配置WebService客户端,然后通过Spring容器来获取WebService客户端实例。示例代码如下: ```xml <jaxws:client id="helloClient" serviceClass="com.example.HelloWorld" address="http://localhost:8080/HelloWorld"/> ``` ```java ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); HelloWorld client = (HelloWorld) context.getBean("helloClient"); String result = client.sayHello("CXF"); ``` 以上是几种常用的调用WebService接口的方式,可以根据具体情况选择适合自己的方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值