zoukankan      html  css  js  c++  java
  • WebService 的CXF框架 WS方式Spring开发

    1.建项目,导包.

     1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     3     <modelVersion>4.0.0</modelVersion>
     4     <groupId>cn_itcast.maven</groupId>
     5     <artifactId>cxf_ws_spring</artifactId>
     6     <version>0.0.1-SNAPSHOT</version>
     7     <name>cxf_ws_spring</name>
     8     <description>CXF的WS整合Spring发布</description>
     9 
    10     <dependencies>
    11         <!-- CXF WS开发 -->
    12         <dependency>
    13             <groupId>org.apache.cxf</groupId>
    14             <artifactId>cxf-rt-frontend-jaxws</artifactId>
    15             <version>3.0.1</version>
    16         </dependency>
    17         <!-- Spring开发 -->
    18         <dependency>
    19             <groupId>org.springframework</groupId>
    20             <artifactId>spring-context</artifactId>
    21             <version>4.1.7.RELEASE</version>
    22         </dependency>
    23 
    24         <dependency>
    25             <groupId>org.springframework</groupId>
    26             <artifactId>spring-web</artifactId>
    27             <version>4.1.7.RELEASE</version>
    28         </dependency>
    29 
    30         <dependency>
    31             <groupId>org.springframework</groupId>
    32             <artifactId>spring-test</artifactId>
    33             <version>4.1.7.RELEASE</version>
    34         </dependency>
    35         <!-- Spring整合junit开发 -->
    36         <dependency>
    37             <groupId>junit</groupId>
    38             <artifactId>junit</artifactId>
    39             <version>4.12</version>
    40         </dependency>
    41 
    42     </dependencies>
    43     <build>
    44         <plugins>
    45             <plugin>
    46                 <groupId>org.codehaus.mojo</groupId>
    47                 <artifactId>tomcat-maven-plugin</artifactId>
    48                 <version>1.1</version>
    49                 <configuration>
    50                     <port>9998</port>
    51                 </configuration>
    52             </plugin>
    53         </plugins>
    54     </build>
    55 </project>
    pom.xml
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3     xmlns="http://java.sun.com/xml/ns/javaee"
     4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     5     id="WebApp_ID" version="2.5">
     6     
     7     <!-- spring配置文件位置 -->
     8     <context-param>
     9         <param-name>contextConfigLocation</param-name>
    10         <param-value>classpath:applicationContext.xml</param-value>
    11     </context-param>
    12     <!-- spring核心监听器 -->
    13     <listener>
    14         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    15     </listener>
    16     <!-- CXF基于web访问 -->
    17     <servlet>
    18         <servlet-name>CXFService</servlet-name>
    19         <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    20         <!-- 加载级别 -->
    21         <load-on-startup>1</load-on-startup>
    22     </servlet>
    23     <servlet-mapping>
    24         <servlet-name>CXFService</servlet-name>
    25         <url-pattern>/services/*</url-pattern>
    26     </servlet-mapping>
    27     <!-- 欢迎页面 -->
    28     <welcome-file-list>
    29         <welcome-file>index.html</welcome-file>
    30         <welcome-file>index.htm</welcome-file>
    31         <welcome-file>index.jsp</welcome-file>
    32         <welcome-file>default.html</welcome-file>
    33         <welcome-file>default.htm</welcome-file>
    34         <welcome-file>default.jsp</welcome-file>
    35     </welcome-file-list>
    36 
    37 </web-app>
    web.xml

    2.导入实体类/service

     1 package cn.itcast.cxf.service;
     2 
     3 import java.util.List;
     4 
     5 import javax.jws.WebMethod;
     6 import javax.jws.WebService;
     7 
     8 import cn.itcast.cxf.domain.Car;
     9 import cn.itcast.cxf.domain.User;
    10 
    11 @WebService
    12 public interface IUserService {
    13     @WebMethod
    14     public String sayHello(String name);
    15 
    16     @WebMethod
    17     public List<Car> findCarsByUser(User user);
    18 }
    IUIservice
     1 package cn.itcast.cxf.service;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 
     6 import javax.jws.WebService;
     7 
     8 import cn.itcast.cxf.domain.Car;
     9 import cn.itcast.cxf.domain.User;
    10 //设置endpointInterface接口服务完整类名,serviceName服务名称.
    11 @WebService(endpointInterface = "cn.itcast.cxf.service.IUserService", serviceName = "userService")
    12 public class UserServiceImpl implements IUserService {
    13 
    14     // 简单参数传递
    15     public String sayHello(String name) {
    16         return "Hello," + name;
    17     }
    18     // 复杂参数传递
    19     public List<Car> findCarsByUser(User user) {
    20         if ("xiaoming".equals(user.getUsername())) {
    21             List<Car> cars = new ArrayList<Car>();
    22             Car car1 = new Car();
    23             car1.setId(1);
    24             car1.setCarName("大众途观");
    25             car1.setPrice(200000d);
    26             cars.add(car1);
    27 
    28             Car car2 = new Car();
    29             car2.setId(2);
    30             car2.setCarName("现代ix35");
    31             car2.setPrice(170000d);
    32             cars.add(car2);
    33 
    34             return cars;
    35         } else {
    36             return null;
    37         }
    38     }
    39 }
    UserviceImpl
     1 package cn.itcast.cxf.domain;
     2 
     3 public class Car {
     4     private Integer id;
     5     private String carName;
     6     private Double price;
     7 
     8     public Integer getId() {
     9         return id;
    10     }
    11 
    12     public void setId(Integer id) {
    13         this.id = id;
    14     }
    15 
    16     public String getCarName() {
    17         return carName;
    18     }
    19 
    20     public void setCarName(String carName) {
    21         this.carName = carName;
    22     }
    23 
    24     public Double getPrice() {
    25         return price;
    26     }
    27 
    28     public void setPrice(Double price) {
    29         this.price = price;
    30     }
    31 
    32     @Override
    33     public String toString() {
    34         return "Car [id=" + id + ", carName=" + carName + ", price=" + price + "]";
    35     }
    36 
    37 }
    Car
     1 package cn.itcast.cxf.domain;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 
     6 public class User {
     7     private Integer id;
     8     private String username;
     9     private String city;
    10 
    11     private List<Car> cars = new ArrayList<Car>();
    12 
    13     public Integer getId() {
    14         return id;
    15     }
    16 
    17     public void setId(Integer id) {
    18         this.id = id;
    19     }
    20 
    21     public String getUsername() {
    22         return username;
    23     }
    24 
    25     public void setUsername(String username) {
    26         this.username = username;
    27     }
    28 
    29     public String getCity() {
    30         return city;
    31     }
    32 
    33     public void setCity(String city) {
    34         this.city = city;
    35     }
    36 
    37     public List<Car> getCars() {
    38         return cars;
    39     }
    40 
    41     public void setCars(List<Car> cars) {
    42         this.cars = cars;
    43     }
    44 
    45 }
    User

    3.配置Springcxf服务发布

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
     4     xsi:schemaLocation="
     5     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     6     http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
     7 
     8     <!-- 
     9         address 客户端访问服务路径 
    10         serviceClass 配置接口
    11         serviceBean 配置实现类 
    12      -->
    13     <jaxws:server id="userService" address="/userService" 
    14         serviceClass="cn.itcast.cxf.service.IUserService">
    15         <jaxws:serviceBean>
    16             <bean class="cn.itcast.cxf.service.UserServiceImpl" />
    17         </jaxws:serviceBean>
    18     </jaxws:server>
    19     
    20 </beans>
    applicationContext.xml

    访问 :http://localhost:9998/cxf_ws_spring/services/userService?wsdl

    4.整合Spring测试,编写客户端

    1).编写applicationContext.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
     4     xsi:schemaLocation="
     5     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     6     http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
     7 
     8     <!-- 
     9         serviceClass 服务接口
    10         address 服务访问地址 
    11      -->
    12     <jaxws:client id="userServiceClient" 
    13         serviceClass="cn.itcast.cxf.service.IUserService" 
    14         address="http://localhost:9998/cxf_ws_spring/services/userService" >
    15         <!-- 来源消息拦截器 -->
    16         <jaxws:inInterceptors>
    17             <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
    18         </jaxws:inInterceptors>
    19         <!-- 输出消息拦截器 -->
    20         <jaxws:outInterceptors>
    21             <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
    22         </jaxws:outInterceptors>
    23     </jaxws:client>
    24 </beans>
    客户端的配置

    2)测试类编写

     1 package cxf_ws_spring;
     2 
     3 import org.junit.Test;
     4 import org.junit.runner.RunWith;
     5 import org.springframework.beans.factory.annotation.Autowired;
     6 import org.springframework.test.context.ContextConfiguration;
     7 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
     8 
     9 import cn.itcast.cxf.service.IUserService;
    10 
    11 @RunWith(SpringJUnit4ClassRunner.class)
    12 @ContextConfiguration(locations = "classpath:applicationContext-test.xml")
    13 public class JAXWS_Spring_Test {
    14     @Autowired
    15     private IUserService proxy;
    16 
    17     @Test
    18     public void testCXF() {
    19         System.out.println(proxy.sayHello("我是程序员"));
    20     }
    21 }
    测试类
  • 相关阅读:
    RTP 与RTCP 解释. 含同步时间戳
    Linux命令之ar
    函数参数的传递问题(指针的指针)
    传指针和传指针引用的区别/指针和引用的区别(本质)
    gdb optimized out错误解决
    实时视频应用之QoS关键技术分析
    中国大陆地区桌面操作系统占比
    一句命令查询 当前目录下面不同文件前缀的数量 并且进行排序
    一致性哈希算法和Go语言实现
    频率之外谁重要?计算机存储系统解析(转载)
  • 原文地址:https://www.cnblogs.com/wwwzzz/p/8036043.html
Copyright © 2011-2022 走看看