Web Service系列(四)Spring整合CXF

Spring整合WebService (Jax-ws)

理解
1. 整合spring也就是把服务接口的代理类交给spring管理
2. Jax-ws实际就是通过代理的方式来实现远程调用服务

服务端

创建web项目 添加依赖
  • 添加CXF依赖
  • 添加spring的整合
  • 使用tomcat7插件
<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.Kato</groupId>
  <artifactId>jaxWs_spring_server</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>服务端</name>

  <!-- 添加CXF依赖 -->
  <dependencies>
     <!-- CXF WS开发  -->
        <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxws</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>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>
    <pluginManagement>
        <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>

            <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>
    </pluginManagement>
  </build>
</project>

web.xml添加配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

    <!-- 配置Spring的监听 -->
    <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服务端,发布服务需要配置CXFServlet
        这里配置的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>

创建服务接口,服务实现,实体类…
  • 服务接口
    • 添加@WebService注解
@WebService
public interface IUserService {
    public String sayHi(String name);
    public List<Car> findCarsByUser(User user);
}
  • 服务实现

public class UserServiceImpl implements IUserService {

    public String sayHi(String name) {
        System.out.println("hi,"+name);
        return "hi,"+name;
    }

    public List<Car> findCarsByUser(User user) {
        if ("宋江".equals(user.getUsername())) {
            List<Car> cars = new ArrayList<Car>();
            Car car1 = new Car();
            car1.setId(1);
            car1.setCarName("大众途观");
            car1.setPrice(200000d);
            cars.add(car1);

            Car car2 = new Car();
            car2.setId(2);
            car2.setCarName("现代ix35");
            car2.setPrice(170000d);
            cars.add(car2);

            return cars;
        } else {
            return null;
        }
    }
}
  • 实体类
public class Car {
    private Integer id;
    private String carName;
    private Double price;
    ......
}
public class User {
    private Integer id;
    private String username;
    private String city;
    ......
}

创建applicationContext.xml 配置CXF配置CXF
  • 注意 : 约束
  • 配置服务address地址
  • 配置服务接口实现bean
<?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整合CXF,发布jaxws服务 服务端
        1. 服务地址
        2. 服务bean
        完整路径:http://localhost:8080/ws/userService
     -->
     <jaxws:server address="/userService">
        <jaxws:serviceBean>
            <bean class="com.service.UserServiceImpl"></bean>
        </jaxws:serviceBean>
     </jaxws:server>

</beans>

启动tomcat发布服务,访问wsdl

image

客户端

创建web项目 添加依赖
  • 添加CXF依赖
  • 添加spring的整合
  • 使用tomcat7插件
<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.Kato</groupId>
  <artifactId>jaxWs_spring_server</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>服务端</name>

  <!-- 添加CXF依赖 -->
  <dependencies>
     <!-- CXF WS开发  -->
        <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxws</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>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>
    <pluginManagement>
        <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>

            <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>
    </pluginManagement>
  </build>
</project>

创建服务接口,服务实现,实体类…
  • 服务接口
    • 添加@WebService注解
@WebService
public interface IUserService {
    public String sayHi(String name);
    public List<Car> findCarsByUser(User user);
}
  • 服务实现

public class UserServiceImpl implements IUserService {

    public String sayHi(String name) {
        System.out.println("hi,"+name);
        return "hi,"+name;
    }

    public List<Car> findCarsByUser(User user) {
        if ("宋江".equals(user.getUsername())) {
            List<Car> cars = new ArrayList<Car>();
            Car car1 = new Car();
            car1.setId(1);
            car1.setCarName("大众途观");
            car1.setPrice(200000d);
            cars.add(car1);

            Car car2 = new Car();
            car2.setId(2);
            car2.setCarName("现代ix35");
            car2.setPrice(170000d);
            cars.add(car2);

            return cars;
        } else {
            return null;
        }
    }
}
  • 实体类
public class Car {
    private Integer id;
    private String carName;
    private Double price;
    ......
}
public class User {
    private Integer id;
    private String username;
    private String city;
    ......
}

创建applicationContext.xml 配置CXF
  • 注意 : 约束
  • 配置服务address地址
  • 配置服务接口实现bean
<?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  指定接口路径,会根据该类型生成代理对象

        完整路径:http://localhost:8080/ws/userService
     -->
     <jaxws:client 
        id="userService"
        address="http://localhost:8080/ws/userService"
        serviceClass="com.service.IUserService"
     ></jaxws:client>

</beans>

测试
  • 测试只能在junit测试了,毕竟用到的都是在项目中直接调用
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class testClient {

    //注入接口userService
    @Resource
    public IUserService userService;

    @Test
    public void test1(){
        System.out.println(userService);
        System.out.println(userService.getClass());

        //远程调用服务接口
        String sayHi = userService.sayHi("Hello");
        System.out.println(sayHi);
        List<Car> findCarsByUser = userService.findCarsByUser(new User("宋江"));
        System.out.println(findCarsByUser);
    }
}
  • 输出
org.apache.cxf.jaxws.JaxWsClientProxy@62727399
class com.sun.proxy.$Proxy45
hi,Hello
[Car [id=1, carName=大众途观, price=200000.0], Car [id=2, carName=现代ix35, price=170000.0]]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值