cxf整合Spring实现webservice调用

cxf整合Spring实现webservice

server端

pom文件内容

<dependencies>
    <!-- CXF WS开发  -->
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-frontend-jaxws</artifactId>
      <version>3.0.1</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.2.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.2.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>4.2.4.RELEASE</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.2</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
          <encoding>UTF-8</encoding>
          <showWarnings>true</showWarnings>
        </configuration>
      </plugin>
      <!-- 运行tomcat7方法:tomcat7:run -->
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <!-- 指定端口 -->
          <port>8080</port>
          <!-- 请求路径 -->
          <path>/</path>
        </configuration>
      </plugin>
    </plugins>
  </build>

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>
  <display-name>Archetype Created Web Application</display-name>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!--webservice服务端,发布服务需要配置CXFSrvlet-->
  <!--这里配置的servlet路径,为最终服务路径的一部分-->
  <!--服务访问路径:http://localhost:8080/web.xml配置路径/spring配置的路
径 -->
  <servlet>
    <servlet-name>cxfServlet</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>cxfServlet</servlet-name>
    <url-pattern>/ws/*</url-pattern>
  </servlet-mapping>
</web-app>

applicationContext.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:cxf="http://cxf.apache.org/core"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xmlns:jaxrs="http://cxf.apache.org/jaxrs"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://cxf.apache.org/core
        http://cxf.apache.org/schemas/core.xsd
        http://cxf.apache.org/jaxws
        http://cxf.apache.org/schemas/jaxws.xsd
        http://cxf.apache.org/jaxrs
        http://cxf.apache.org/schemas/jaxrs.xsd">

    <!--
    1. 服务地址
    2. 服务bean
    完整服务地址:
    http://localhost:8080/ws/userService
    Spring整合ApacheCXF发布jaxws服务
	-->
    <jaxws:server address="/userService">
        <jaxws:serviceBean>
            <bean class="com.sks.service.impl.UserServiceImpl"/>
        </jaxws:serviceBean>
    </jaxws:server>
    
</beans>

接口

@WebService
public interface UserService {

    public String sayHello(String name, int age);
}

接口实现类

public class UserServiceImpl implements UserService {

    @Override
    public String sayHello(String name, int age) {
        return "hello " + name + " " + age;
    }
}

最后点击如图所示的按钮运行项目。
在这里插入图片描述
在浏览器中输入http://localhost:8080/ws/userService?wsdl,访问wsdl说明书,可以看到以下内容:
在这里插入图片描述

client端

项目目录结构,web.xml文件中的内容不需要改动
在这里插入图片描述

applicationContext.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:cxf="http://cxf.apache.org/core"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xmlns:jaxrs="http://cxf.apache.org/jaxrs"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://cxf.apache.org/core
        http://cxf.apache.org/schemas/core.xsd
        http://cxf.apache.org/jaxws
        http://cxf.apache.org/schemas/jaxws.xsd
        http://cxf.apache.org/jaxrs
        http://cxf.apache.org/schemas/jaxrs.xsd">


    <!--Spring整合ApacheCXF,客户端配置
    关键点:
    通过Spring整合ApacheCXF,创建客户端的代理对象,远程访问服务端。
    jaxws:client
    id 应用中注入的接口的代理对象的名称
    address 服务端访问地址
    serviceClass 指定接口路径,会根据该类型生成代理对象
    -->
    <jaxws:client id="userService" address="http://localhost:8080/ws/userService" serviceClass="com.sks.service.UserService">

    </jaxws:client>

</beans>

测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Test1 {

    @Resource
    private UserService userService;

    @Test
    public void test1() {
        System.out.println(userService);
        System.out.println(userService.getClass());
        //远程服务调用接口
        String context = userService.sayHello("球球",19);
        System.out.println(context);
    }
}

点击运行以后控制台打印输出结果如下,这说明我们的调用成功了。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值