(四)整合spring与cxf的webservice开发

下面以客户端通过id查询服务器端的User为例。

<一服务器端开发>

1、建立一个动态工程webservice-cxf-spring-server,并把cxf框架下lib目录中的jar包导入到/webservice-cxf-spring-server/WebContent/WEB-INF/lib中, 配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>webservice-cxf-spring-server</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <!-- 配置beans.xml,指定bean.xml的加载位置-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:beans.xml</param-value>
  </context-param>

  <!-- 应用启动的一个监听器,监听webservice的请求和响应时,当有请求和响应时就用cxf的框架进行处理 -->
  <listener>
    <listener-class>
         org.springframework.web.context.ContextLoaderListener
      </listener-class>
  </listener>

  <!-- 表示所有请求都会先经过cxf框架 -->
  <servlet>
    <servlet-name>CXFServlet</servlet-name>
    <servlet-class>
         org.apache.cxf.transport.servlet.CXFServlet
      </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>CXFServlet</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

</web-app>

2、在src目录下创建服务端spring整合cxf的配置文件bean.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:context="http://www.springframework.org/schema/context"
    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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

    <!-- 引入cxf的一些核心配置 -->
    <!-- 
    cxf.xml配置cxf的一些核心处理器;
    cxf-extension-soap.xml配置cxf的一些扩展功能;
    cxf-servlet.xml用于自定义cxf的特性,默认为空
     -->
   <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" /> 

   <!-- 用来发布服务 -->
   <!-- 
    address表示服务的地址,本例为http://localhost:8080/webservice-cxf-spring-server/userService;
    对应的wsdl地址为http://localhost:8080/webservice-cxf-spring-server/userService?wsdl
    -->
   <jaxws:endpoint 
     id="userService" 
     implementor="com.lzj.cxf.springImpl.UserServiceImpl" 
     address="/userService">
    </jaxws:endpoint>
</beans>

3、建立服务端响应客户端的服务接口UserService

package com.lzj.cxf.spring;
import javax.jws.WebMethod;
import javax.jws.WebService;
import com.lzj.bean.User;

@WebService
public interface UserService {

    @WebMethod
    public User findUserById(int id);

}

其中User对应的bean为

package com.lzj.bean;

public class User {

    private int id;
    private String name;
    private int age;

    public User() {
        super();
    }

    public User(int id, String name, int age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    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 "User [id=" + id + ", name=" + name + ", age=" + age + "]";
    }

}

4、创建服务接口UserService的实现UserServiceImpl

package com.lzj.cxf.springImpl;
import java.util.Arrays;
import java.util.List;
import javax.jws.WebService;
import com.lzj.bean.User;
import com.lzj.cxf.spring.UserService;

@WebService
public class UserServiceImpl implements UserService{

    private static List<User> users = Arrays.asList(
            new User(1, "lzj", 25),
            new User(2, "Tom", 20),
            new User(30, "Bob", 30));

    /*当启动工程后,会自动加载UserServiceImpl服务,因此启动工程的时候就会执行该构造器*/
    public UserServiceImpl(){
        System.out.println("加载服务!");
    }

    @Override
    public User findUserById(int id) {
        /*根据id找出users列表中的User对应的对象*/
        User user = users.stream()
                .filter((x) -> x.getId() == id)
                .findAny()
                .get();
        return user;
    }

}

<二客户端开发>

1、创建客户端的web工程,把cxf框架下lib目录中的jar包导入到/webservice-cxf-spring-client/WebContent/WEB-INF/lib中,并用wsdl2java命令生成代码。(注意:要首先启动服务器端才用该命令生成代码)。
在cmd中进入客户端工程的src目录,执行

wsdl2java http://localhost:8080/webservice-cxf-spring-server/userService?wsdl

生成代码如下
这里写图片描述

2、在src目录下创建客户端的spring整合cxf的配置文件bean.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://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--
    其中serviceClass指定服务器端响应客户端的服务接口;
    address指定服务器中服务的地址
    -->
    <jaxws:client id="userService" 
        serviceClass= "com.lzj.cxf.spring.UserService" 
        address= "http://localhost:8080/webservice-cxf-spring-server/userService">
    </jaxws:client>

</beans>

3、在客户端创建测试方法,用于请求服务器端的服务

package com.lzj.cxf.spring.client.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lzj.cxf.spring.User;
import com.lzj.cxf.spring.UserService;

public class ClientTest {

    public static void main(String[] args) {
        /*如果有多个配置文件,可以同时添加多个配置文件*/
        ClassPathXmlApplicationContext context = 
                new ClassPathXmlApplicationContext(new String[]{"classpath:bean.xml"});
        UserService userService = (UserService) context.getBean("userService");
        User user = userService.findUserById(1);
        System.out.println(user);
    }

}

<三响应结果>

先启动服务器端工程,输出log如下:

……
加载服务!
……

然后运行客户端的测试方法,输出log如下:

User [age=25, id=1, name=lzj]

可见,服务器端正确响应了客户端的请求。

服务器端工程目录
这里写图片描述

客户端工程目录
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值