侧边栏壁纸
博主头像
张种恩的技术小栈博主等级

行动起来,活在当下

  • 累计撰写 748 篇文章
  • 累计创建 65 个标签
  • 累计收到 39 条评论

目 录CONTENT

文章目录

Spring集成CXF简单调用

zze
zze
2018-07-07 / 0 评论 / 0 点赞 / 695 阅读 / 7885 字

不定期更新相关视频,抖音点击左上角加号后扫一扫右方侧边栏二维码关注我~正在更新《Shell其实很简单》系列

简介

Apache CXF = Celtix + XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF。CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且提供了多种 Binding 、DataBinding、Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code First)或者 WSDL 优先(WSDL First)来轻松地实现 Web Services 的发布和使用。Apache CXF 已经是一个正式的 Apache 顶级项目。

使用

服务端编写

1、新建 Maven Web 工程作为服务端,引入如下依赖:

<!-- pom.xml -->
<?xml version="1.0" encoding="UTF-8"?>

<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.zze</groupId>
  <artifactId>cxf_server</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <dependencies>
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-frontend-jaxws</artifactId>
      <version>3.0.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http</artifactId>
      <version>3.0.1</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.2.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.2.4.RELEASE</version>
    </dependency>
  </dependencies>
</project>

2、新建服务类接口及其实现类:

// com.zze.webservice.ITestService
import javax.jws.WebService;
@WebService
public interface ITestService {
    String hello();
}
// com.zze.webservice.impl.TestService
import com.zze.webservice.ITestService;

public class TestService implements ITestService {
    public String hello() {
        return "hello CXF";
    }
}

3、在 Spring 配置文件中发布服务:

<!-- applicationContext_cxf.xml -->
<?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"
       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">
    <!--调用的类-->
    <bean id="testService" class="com.zze.webservice.impl.TestService"></bean>
    <!--发布服务-->
    <jaxws:server address="/testService">
        <jaxws:serviceBean>
            <ref bean="testService"/>
        </jaxws:serviceBean>
    </jaxws:server>
</beans>

4、配置 Spring 监听器及 CXF 过滤器:

<!-- WEB-INF/web.xml -->
!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext_cxf.xml</param-value>
    </context-param>
    <filter>
        <filter-name>cxf</filter-name>
        <filter-class>org.apache.cxf.transport.servlet.CXFServlet</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>cxf</filter-name>
        <url-pattern>/ws/*</url-pattern>
    </filter-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

5、启动程序(服务随之发布),测试服务是否发布成功,发布成功则如下:

image.png

编写客户端

1、使用 Maven 新建普通 Java 工程作为客户端,引入如下依赖:

<!-- pom.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<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.zze</groupId>
    <artifactId>cxf_client</artifactId>
    <version>1.0-SNAPSHOT</version>

   <dependencies>
       <dependency>
           <groupId>org.apache.cxf</groupId>
           <artifactId>cxf-rt-frontend-jaxws</artifactId>
           <version>3.0.1</version>
       </dependency>
       <dependency>
           <groupId>org.apache.cxf</groupId>
           <artifactId>cxf-rt-transports-http</artifactId>
           <version>3.0.1</version>
       </dependency>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-context</artifactId>
           <version>4.2.4.RELEASE</version>
       </dependency>
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.12</version>
           <scope>test</scope>
       </dependency>
   </dependencies>
</project>

2、cmd 进入 classpath 目录下,执行如下命令生成客户端代码:

wsimport -s . http://localhost:8080/ws/testService?wsdl

3、在 Spring 配置文件中配置调用服务的客户端实例:

<!-- applicationContext_cxf.xml -->
<?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"
       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">
    <jaxws:client id="testClient" address="http://localhost:8080/ws/testService?wsdl"
                  serviceClass="com.zze.webservice.impl.ITestService"/>
</beans>

4、编写代码调用服务端,如下输出则调用成功:

// com.zze.webservice.test.CXFTest
import com.zze.webservice.impl.ITestService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CXFTest{
    @Test
    public void test1() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext_cxf.xml");
        ITestService testClient = (ITestService)applicationContext.getBean("testClient");
        System.out.println(testClient.hello());
    }
}

image.png

0

评论区