cxf 开发客户端

开发工具准备:

1apache-cxf-2.2.6.zip

2spring-ws-1.5.8.zip

3eclipse-jee-galileo-SR1-win32

 

开发步骤:

一、新建一个普通的java工程,名字叫WebService_CXF_Client

二、导入apache-cxf-2.2.6 spring-ws-1.5.8 下的jar

三、启动《CXF开发WebService服务器端》所开发的WebService服务器

四、进入apache-cxf-2.2.6/bin文件夹内,运行

 wsdl2java.bat http://localhost:8080/WebService_CXF_Host/service/HelloWorld?wsdl

将会在bin文件夹内生成一个demo文件夹

注意,可以查看demo.sprint文件夹内的HelloWorld.java接口,定义的aryMyObjects方法的返回值是List<MyObject>类型,尽管在服务器端定义接口的返回值类型是数组.

五、将整个demo文件夹剪切到WebService_CXF_Client工程的src目录下

六、src目录下新建一个client-beans.xml文件,内容如下:

<?xmlversion="1.0"encoding="UTF-8"?>

 

<beansxmlns="http://www.springframework.org/schema/beans"

       xmlns:jaxws="http://cxf.apache.org/jaxws"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

 

    <beanid="client"class="demo.spring.HelloWorld"

      factory-bean="clientFactory"factory-method="create"/>

   

    <beanid="clientFactory"class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">

      <propertyname="serviceClass"value="demo.spring.HelloWorld"/>

      <propertyname="address"value="http://localhost:8080/WebService_CXF_Host/service/HelloWorld"/>

    </bean>

 

      <jaxws:clientid="client2"

                  serviceClass="demo.spring.HelloWorld"

                  address="http://localhost:8080/WebService_CXF_Host/service/HelloWorld"/>

</beans>

 

注意:client client2 是两种访问webservice的写法,效果一样。

 

七、新建含main方法的Client.java类,内容中下:

 

import java.util.ArrayList;

import java.util.List;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import demo.spring.HelloWorld;

import demo.spring.MyObject;

 

 

public final class Client {

 

    private Client() {

    }

 

    public static void main(String args[]) throws Exception {

        // START SNIPPET: client

        ClassPathXmlApplicationContext context

            = new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"});

 

        HelloWorld client = (HelloWorld)context.getBean("client2");

 

        System.out.println("begin stringInt method");

        int num=999;

        String text="i love you";

        String response = client.stringInt(text, num);

        System.out.println("Response: " + response);

       

        System.out.println("begin aryMyObjects method");

        List<MyObject> lstSource=new ArrayList<MyObject>();

        MyObject myObject1=new MyObject();

        myObject1.setId(0);

        myObject1.setName("000");

        MyObject myObject2=new MyObject();

        myObject2.setId(1);

        myObject2.setName("111");

        lstSource.add(myObject1);

        lstSource.add(myObject2);

       

        List<MyObject> lstResponse=client.aryMyObjects(lstSource);

        for(int i=0;i<lstResponse.size();i++){

                 System.out.println("id: "+lstResponse.get(i).getId()+"   name: "+lstResponse.get(i).getName());

        }

       

        System.exit(0);

       }

}

 

至此,客户端也成功开发了

您好!关于使用Spring Boot和Apache CXF开发客户端的问题,我可以为您提供一些指导。 首先,您需要在您的Spring Boot项目中添加CXF的依赖。可以在您的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.4.1</version> </dependency> ``` 接下来,您可以创建一个CXF客户端来调用远程WebService。您可以使用`@WebServiceClient`注解来生成客户端代码。例如,假设您要调用一个名为"HelloWorldService"的WebService: ```java @WebServiceClient(name = "HelloWorldService", targetNamespace = "http://example.com/", wsdlLocation = "http://example.com/HelloWorldService?wsdl") public class HelloWorldServiceClient extends Service { public HelloWorldServiceClient(URL wsdlLocation, QName serviceName) { super(wsdlLocation, serviceName); } public HelloWorldService getHelloWorldServicePort() { return super.getPort(HelloWorldService.class); } } ``` 在上面的代码中,`wsdlLocation`参数指定了远程WebService的WSDL地址。`getHelloWorldServicePort()`方法返回了一个用于调用WebService方法的接口。 接下来,您可以在您的Spring Boot应用程序中使用这个客户端。您可以将它注入到您的服务或控制器中,并使用它来调用远程WebService方法。例如: ```java @Service public class MyService { @Autowired private HelloWorldServiceClient helloWorldServiceClient; public void invokeRemoteService() { HelloWorldService helloWorldService = helloWorldServiceClient.getHelloWorldServicePort(); String result = helloWorldService.sayHello("World"); System.out.println(result); } } ``` 在上面的代码中,通过`helloWorldServiceClient.getHelloWorldServicePort()`方法获取到了远程WebService的接口实例,然后可以调用其中的方法。 这就是使用Spring Boot和CXF开发客户端的基本步骤。您可以根据您的具体需求进一步进行配置和调整。希望对您有所帮助!如果您有更多问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值