cxf的soap风格+spirng4+maven 客户端

上篇博客介绍了,cxf的soap风格的服务端,现在我们写客户端来调用

1、pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.jacky</groupId>
  <artifactId>cxf_client</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>cxf_client</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-common</artifactId>
            <version>2.5.4</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-core</artifactId>
            <version>2.6.1</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>2.6.1</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
  </dependencies>
</project>

2.Info.xml

package com.jacky;

public class Info {
    private String name;  
    private int age;  
    public String getName()  
    {  
        return name;  
    }  
    public void setName(String name)  
    {  
        this.name = name;  
    }  
    public int getAge()  
    {  
        return age;  
    }  
    public void setAge(int age)  
    {  
        this.age = age;  
    }
    @Override
    public String toString() {
        return "Info [name=" + name + ", age=" + age + "]";
    }  
    
}

3、App.java

package com.jacky;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.junit.Test;

import com.jacky.service.GetInfoService;

/**
 * Hello world!
 *
 */
public class App {
 /**
  * 简介:调用方式采用了和RMI类似的机制,即客户端直接服务器端提供的服务接口(interface),
  *       CXF通过运行时代理生成远程服务的代理对象,
  *       在客户端完成对webservice的访问;几个必填的字段:
  *       setAddress-这个就是我们发布webservice时候的地址,保持一致。
      缺点:这种调用service的好处在于调用过程非常简单,就几行代码就完成一个webservice的调用,
  *      但是客户端也必须依赖服务器端的接口,这种调用方式限制是很大的,
  *      要求服务器端的webservice必须是java实现--这样也就失去了使用webservice的意义。
  */
  @Test      
  public void test1(){
       JaxWsProxyFactoryBean bean = new JaxWsProxyFactoryBean();  
       bean.setServiceClass(GetInfoService.class);  
       bean.setAddress("http://localhost:8080/cxf_demo/cxf/getInfoService");  
       GetInfoService service = (GetInfoService)bean.create();
       int result = service.add(1, 1);
       System.out.println("result====="+result);
       Info info = service.getInfo("罗志茂", 21);
       System.out.println("info==="+info);  
      }
   /**
    * 简介:只要指定服务器端wsdl文件的位置,然后指定要调用的方法和方法的参数即可,不关心服务端的实现方式。
    *    wsdl [Web Services Description Language]网络服务描述语言是Web Service的描述语言,它包含一系列描述某个web service的定义
    * @throws Exception
    */
    @Test
    public void test2() throws Exception {
        JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();  
        Client client = clientFactory.createClient("http://localhost:8080/cxf_demo/cxf/getInfoService?wsdl"); 
        Object[] objs ={1,2};
        Object[] result = client.invoke("add", objs);  
        System.out.println(result[0]);  
    }
}

在这里cxf soap客户端的两种调用方式就写好了

链接:http://pan.baidu.com/s/1mi376Es 密码:ljy6

欢迎关注

原文地址:https://www.cnblogs.com/520playboy/p/6227930.html