zoukankan      html  css  js  c++  java
  • CXF——WebService简介,CXF框架的使用

    WebService

    Web service是一个平台独立的,低耦合的,自包含的、基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述、发布、发现、协调和配置这些应用程序,用于开发分布式的互操作的应用程序。

    Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的、专门的第三方软件或硬件, 就可相互交换数据或集成。依据Web Service规范实施的应用之间, 无论它们所使用的语言、 平台或内部协议是什么, 都可以相互交换数据。Web Service是自描述、 自包含的可用网络模块, 可以执行具体的业务功能。Web Service也很容易部署, 因为它们基于一些常规的产业标准以及已有的一些技术,诸如标准通用标记语言下的子集XMLHTTPWeb Service减少了应用接口的花费。Web Service为整个企业甚至多个组织之间的业务流程的集成提供了一个通用机制。

    WebService的特点

    • WebService通过HTTP POST方式接受客户的请求
    • WebService与客户端之间一般使用SOAP协议传输XML数据
    • 它本身就是为了跨平台或跨语言而设计的

    使用apache CXF发布一个WebService

    1、创建数据库并建表

     1 /*
     2 Navicat MySQL Data Transfer
     3 
     4 Source Server         : root
     5 Source Server Version : 50022
     6 Source Host           : localhost:3306
     7 Source Database       : crm
     8 
     9 Target Server Type    : MYSQL
    10 Target Server Version : 50022
    11 File Encoding         : 65001
    12 
    13 Date: 2015-04-19 17:46:45
    14 */
    15 
    16 SET FOREIGN_KEY_CHECKS=0;
    17 
    18 -- ----------------------------
    19 -- Table structure for `t_customer`
    20 -- ----------------------------
    21 DROP TABLE IF EXISTS `t_customer`;
    22 CREATE TABLE `t_customer` (
    23   `id` int(11) NOT NULL auto_increment,
    24   `name` varchar(255) default NULL,
    25   `station` varchar(255) default NULL,
    26   `telephone` varchar(255) default NULL,
    27   `address` varchar(255) default NULL,
    28   `decidedzone_id` varchar(255) default NULL,
    29   PRIMARY KEY  (`id`)
    30 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    31 
    32 -- ----------------------------
    33 -- Records of t_customer
    34 -- ----------------------------
    35 INSERT INTO `t_customer` VALUES ('1', '张三', '百度', '13811111111', '北京市西城区长安街100号', null);
    36 INSERT INTO `t_customer` VALUES ('2', '李四', '哇哈哈', '13822222222', '上海市虹桥区南京路250号', null);
    37 INSERT INTO `t_customer` VALUES ('3', '王五', '搜狗', '13533333333', '天津市河北区中山路30号', null);
    38 INSERT INTO `t_customer` VALUES ('4', '赵六', '联想', '18633333333', '石家庄市桥西区和平路10号', null);
    39 INSERT INTO `t_customer` VALUES ('5', '小白', '测试空间', '18511111111', '内蒙古自治区呼和浩特市和平路100号', null);
    40 INSERT INTO `t_customer` VALUES ('6', '小黑', '联想', '13722222222', '天津市南开区红旗路20号', null);
    41 INSERT INTO `t_customer` VALUES ('7', '小花', '百度', '13733333333', '北京市东城区王府井大街20号', null);
    42 INSERT INTO `t_customer` VALUES ('8', '小李', '长城', '13788888888', '北京市昌平区建材城西路100号', null);
    建表语句

    2、配置maven的配置文件

      1 <?xml version="1.0" encoding="UTF-8"?>
      2 <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/maven-v4_0_0.xsd">
      3 
      4   <modelVersion>4.0.0</modelVersion>
      5   <packaging>war</packaging>
      6 
      7   <name>crm</name>
      8   <groupId>cn.x5456</groupId>
      9   <artifactId>crm</artifactId>
     10   <version>1.0-SNAPSHOT</version>
     11 
     12   <!-- 通过属性定义指定jar的版本 -->
     13   <properties>
     14     <spring.version>4.3.14.RELEASE</spring.version>
     15     <hibernate.version>5.0.7.Final</hibernate.version>
     16     <struts2.version>2.3.24</struts2.version>
     17     <slf4j.version>1.6.6</slf4j.version>
     18     <log4j.version>1.2.12</log4j.version>
     19     <shiro.version>1.2.3</shiro.version>
     20   </properties>
     21 
     22   <dependencies>
     23     <dependency>
     24       <groupId>org.springframework</groupId>
     25       <artifactId>spring-context</artifactId>
     26       <version>${spring.version}</version>
     27     </dependency>
     28 
     29     <dependency>
     30       <groupId>org.springframework</groupId>
     31       <artifactId>spring-context-support</artifactId>
     32       <version>${spring.version}</version>
     33     </dependency>
     34 
     35     <dependency>
     36       <groupId>org.springframework</groupId>
     37       <artifactId>spring-web</artifactId>
     38       <version>${spring.version}</version>
     39     </dependency>
     40 
     41     <dependency>
     42       <groupId>org.springframework</groupId>
     43       <artifactId>spring-orm</artifactId>
     44       <version>${spring.version}</version>
     45     </dependency>
     46 
     47     <dependency>
     48       <groupId>org.springframework</groupId>
     49       <artifactId>spring-beans</artifactId>
     50       <version>${spring.version}</version>
     51     </dependency>
     52 
     53     <dependency>
     54       <groupId>org.springframework</groupId>
     55       <artifactId>spring-core</artifactId>
     56       <version>${spring.version}</version>
     57     </dependency>
     58 
     59     <dependency>
     60       <groupId>org.aspectj</groupId>
     61       <artifactId>aspectjweaver</artifactId>
     62       <version>1.7.4</version>
     63     </dependency>
     64 
     65     <!-- struts2 begin -->
     66     <dependency>
     67       <groupId>org.apache.struts</groupId>
     68       <artifactId>struts2-core</artifactId>
     69       <version>${struts2.version}</version>
     70       <!-- 排除传递的依赖 -->
     71       <exclusions>
     72         <exclusion>
     73           <artifactId>javassist</artifactId>
     74           <groupId>javassist</groupId>
     75         </exclusion>
     76       </exclusions>
     77     </dependency>
     78     <dependency>
     79       <groupId>org.apache.struts</groupId>
     80       <artifactId>struts2-spring-plugin</artifactId>
     81       <version>${struts2.version}</version>
     82     </dependency>
     83     <dependency>
     84       <groupId>org.apache.struts</groupId>
     85       <artifactId>struts2-convention-plugin</artifactId>
     86       <version>${struts2.version}</version>
     87     </dependency>
     88     <!-- struts2 end -->
     89 
     90     <!-- hibernate begin -->
     91     <dependency>
     92       <groupId>org.hibernate</groupId>
     93       <artifactId>hibernate-core</artifactId>
     94       <version>${hibernate.version}</version>
     95     </dependency>
     96     <!-- hibernate end -->
     97 
     98     <!-- log start -->
     99     <dependency>
    100       <groupId>log4j</groupId>
    101       <artifactId>log4j</artifactId>
    102       <version>${log4j.version}</version>
    103     </dependency>
    104 
    105     <dependency>
    106       <groupId>org.slf4j</groupId>
    107       <artifactId>slf4j-api</artifactId>
    108       <version>${slf4j.version}</version>
    109     </dependency>
    110 
    111     <dependency>
    112       <groupId>org.slf4j</groupId>
    113       <artifactId>slf4j-log4j12</artifactId>
    114       <version>${slf4j.version}</version>
    115     </dependency>
    116     <!-- log end -->
    117 
    118     <dependency>
    119       <groupId>org.apache.poi</groupId>
    120       <artifactId>poi</artifactId>
    121       <version>3.11</version>
    122     </dependency>
    123 
    124     <dependency>
    125       <groupId>org.apache.cxf</groupId>
    126       <artifactId>cxf-rt-frontend-jaxws</artifactId>
    127       <version>3.1.5</version>
    128     </dependency>
    129     <dependency>
    130       <groupId>org.apache.cxf</groupId>
    131       <artifactId>cxf-rt-transports-http</artifactId>
    132       <version>3.1.5</version>
    133     </dependency>
    134     <dependency>
    135       <groupId>org.apache.cxf</groupId>
    136       <artifactId>cxf-rt-frontend-jaxrs</artifactId>
    137       <version>3.1.5</version>
    138     </dependency>
    139     <dependency>
    140       <groupId>junit</groupId>
    141       <artifactId>junit</artifactId>
    142       <version>4.10</version>
    143       <scope>test</scope>
    144     </dependency>
    145 
    146     <!-- 加入servlet和jsp的依赖 -->
    147     <dependency>
    148       <groupId>javax.servlet</groupId>
    149       <artifactId>servlet-api</artifactId>
    150       <version>2.5</version>
    151       <scope>provided</scope>
    152     </dependency>
    153     <dependency>
    154       <groupId>javax.servlet</groupId>
    155       <artifactId>jsp-api</artifactId>
    156       <version>2.0</version>
    157       <scope>provided</scope>
    158     </dependency>
    159 
    160     <!-- 引入pinyin4J的依赖 -->
    161     <dependency>
    162       <groupId>com.belerweb</groupId>
    163       <artifactId>pinyin4j</artifactId>
    164       <version>2.5.0</version>
    165     </dependency>
    166 
    167     <!-- 引入json-lib的依赖 -->
    168     <dependency>
    169       <groupId>net.sf.json-lib</groupId>
    170       <artifactId>json-lib</artifactId>
    171       <version>2.4</version>
    172     </dependency>
    173 
    174     <!-- 引入c3p0jar包 -->
    175     <dependency>
    176       <groupId>c3p0</groupId>
    177       <artifactId>c3p0</artifactId>
    178       <version>0.9.1.2</version>
    179     </dependency>
    180 
    181     <!-- 引入ehcache的依赖 -->
    182     <dependency>
    183       <groupId>net.sf.ehcache</groupId>
    184       <artifactId>ehcache-core</artifactId>
    185       <version>2.6.6</version>
    186     </dependency>
    187     <!-- 引入shiro框架的依赖 -->
    188     <dependency>
    189       <groupId>org.apache.shiro</groupId>
    190       <artifactId>shiro-all</artifactId>
    191       <version>1.2.2</version>
    192     </dependency>
    193     <!-- 引入MySQL数据库驱动依赖 -->
    194     <dependency>
    195       <groupId>mysql</groupId>
    196       <artifactId>mysql-connector-java</artifactId>
    197       <version>5.1.32</version>
    198     </dependency>
    199     <dependency>
    200       <groupId>commons-fileupload</groupId>
    201       <artifactId>commons-fileupload</artifactId>
    202       <version>1.3.1</version>
    203     </dependency>
    204     <dependency>
    205       <groupId>commons-io</groupId>
    206       <artifactId>commons-io</artifactId>
    207       <version>2.0.1</version>
    208     </dependency>
    209 
    210     <!--坑爹的Java9-->
    211 
    212     <!--<dependency>-->
    213       <!--<groupId>javax.xml.bind</groupId>-->
    214       <!--<artifactId>jaxb-api</artifactId>-->
    215       <!--<version>2.2.12</version>-->
    216     <!--</dependency>-->
    217 
    218     <!--<dependency>-->
    219       <!--<groupId>javax.activation</groupId>-->
    220       <!--<artifactId>activation</artifactId>-->
    221       <!--<version>1.1.1</version>-->
    222     <!--</dependency>-->
    223 
    224   </dependencies>
    225 
    226   <build>
    227     <!-- 插件 -->
    228     <plugins>
    229       <plugin>
    230         <groupId>org.apache.maven.plugins</groupId>
    231         <artifactId>maven-resources-plugin</artifactId>
    232         <version>2.5</version>
    233       </plugin>
    234       <plugin>
    235         <groupId>org.apache.maven.plugins</groupId>
    236         <artifactId>maven-compiler-plugin</artifactId>
    237         <version>2.3.2</version>
    238         <configuration>
    239           <source>1.7</source>
    240           <target>1.7</target>
    241         </configuration>
    242       </plugin>
    243       <!-- 引入tomcat插件 -->
    244       <plugin>
    245         <groupId>org.apache.tomcat.maven</groupId>
    246         <artifactId>tomcat7-maven-plugin</artifactId>
    247         <version>2.2</version>
    248         <configuration>
    249           <path>/crm</path>
    250           <port>8889</port>
    251         </configuration>
    252       </plugin>
    253     </plugins>
    254     <resources>
    255       <resource>
    256         <directory>src/main/java</directory>
    257         <includes>
    258           <include>**/*.properties</include>
    259           <include>**/*.xml</include>
    260         </includes>
    261         <filtering>false</filtering>
    262       </resource>
    263       <resource>
    264         <directory>src/main/resources</directory>
    265         <includes>
    266           <include>**/*.properties</include>
    267           <include>**/*.xml</include>
    268         </includes>
    269         <filtering>false</filtering>
    270       </resource>
    271     </resources>
    272   </build>
    273 
    274 </project>
    View Code

    3、配置web.xml文件(2.*与3.*有区别)

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
        
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:cxf.xml</param-value>
        </context-param>
        
        
        <!--配置Spring的监听器-->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <servlet>
            <servlet-name>cxf</servlet-name>
            <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <!--配置CXF框架提供的Servlet -->
        <servlet-mapping>
            <servlet-name>cxf</servlet-name>
            <url-pattern>/service/*</url-pattern>
        </servlet-mapping>
        
    </web-app>

    4、在类路径下创建cxf.xml文件(2.*与3.*有区别)

    <?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"
           xmlns:soap="http://cxf.apache.org/bindings/soap" xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
    					http://www.springframework.org/schema/beans/spring-beans.xsd
    					http://cxf.apache.org/bindings/soap
    					http://cxf.apache.org/schemas/configuration/soap.xsd
    					http://cxf.apache.org/jaxws
    					http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!--1.创建连接池对象,使用SpringJDBC自带的-->
        <bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql:///CRM"/>
            <property name="username" value="root"/>
            <property name="Password" value="5456"/>
        </bean>
        <!--2.将CustomerServiceImpl放入Spring容器中,由于继承了JdbcDaoSupport类,所以要注入连接池对象-->
        <bean name="customerService" class="cn.x5456.service.CustomerServiceImpl">
            <property name="dataSource" ref="dataSource"/>
        </bean>
        <!-- 3.配置事务核心管理器,封装了所有事务操作. 依赖于连接池 -->
        <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
            <property name="dataSource" ref="dataSource" />
        </bean>
        <!-- 4.开启使用注解管理aop事务 -->
        <context:component-scan base-package="cn.x5456" />
        <tx:annotation-driven/>
        <!-- 5.注册服务 -->
            <!--cxf2.*版本-->
        <!--<jaxws:server id="myService" address="/customer">-->
            <!--<jaxws:serviceBean>-->
                <!--<ref bean="customerService"/>-->
            <!--</jaxws:serviceBean>-->
        <!--</jaxws:server>-->
            <!--cxf3.*版本-->
        <jaxws:endpoint id="myService" implementor="#customerService" address="/customer" />
    </beans>
    

    5、根据表创建一个实体类

    public class Customer {
    
        private Integer id;
        private String name;
        private String station;
        private String telephone;
        private String address;
        private String decidedzone_id;
    }
    

    6、开发一个接口,和实现类

    接口

    // @WebService这个注解一定要写在接口上
    @WebService
    public interface ICustomerService {
        List<Customer> findAll();
    }

    实现类

    @Transactional
    public class CustomerServiceImpl extends JdbcDaoSupport implements ICustomerService {
        @Override
        public List<Customer> findAll() {
    
            String sql = "select * from t_customer";
    
            List<Customer> customerList = super.getJdbcTemplate().query(sql, new RowMapper<Customer>() {
                @Override
                public Customer mapRow(ResultSet resultSet, int i) throws SQLException {
                    Customer customer = new Customer();
    
                    customer.setId(resultSet.getInt("id"));
                    customer.setName(resultSet.getString("name"));
                    customer.setStation(resultSet.getString("station"));
                    customer.setTelephone(resultSet.getString("telephone"));
                    customer.setAddress(resultSet.getString("address"));
                    return customer;
                }
            });
    
            return customerList;
        }
    }
    

    7、执行命令,创建服务器端的代码

    将生成的代码copy到测试项目的src目录下

    8、书写测试类

    public class test {
    
        @Test
        public void func(){
            CustomerServiceImplService ss = new CustomerServiceImplService();
    
            ICustomerService proxy = ss.getCustomerServiceImplPort();
    
            List<Customer> customerList = proxy.findAll();
    
            System.out.println(customerList);
        }
        
    }
    
  • 相关阅读:
    清除页面广告?身为前端,自己做一款简易的Chrome扩展吧
    Nginx 引入线程池,提升 9 倍性能
    调试时屏蔽JavaScript库代码 –Chrome DevTools Blackbox功能介绍
    收集的React.JS资料
    谈谈 React.js 的核心入门知识
    同时包含字母和数字的正则表达式
    Word2010撤销按钮失效,Ctrl+Z失效解决办法
    大数据于产业金融领域的运用究竟如何很好的实现
    HTTP 错误 500.19
    WindowsServer2012桌面图标设置
  • 原文地址:https://www.cnblogs.com/x54256/p/8592875.html
Copyright © 2011-2022 走看看