• cxf webservice开发

    普通类
    • 支持
    • 批判
    • 提问
    • 解释
    • 补充
    • 删除
    • 1.下载相关jar包

    可以到cxf.apache.org 下载相关jar包

    • 2.编写服务器代码

    1.编写服务接口

    //定义了2个方法

    package com.webservice.endpoint;

    import javax.jws.WebService;

    @WebService

    public interface Hello {

    public String hello(String name);

    public String checkUser(String userName, String password);

    }

    2.实现服务接口

    package com.webservice.endpoint.impl;

    import javax.jws.WebService;

    import com.webservice.endpoint.Hello;

    @WebService(endpointInterface = "com.webservice.endpoint.Hello")

    public class HelloServiceImpl implements Hello {

    @Override

    public String hello(String userName) {

    // TODO Auto-generated method stub

    return "Hello Spring!" + userName;

    }

    @Override

    public String checkUser(String userName, String password) {

    if (userName.equals("abc")) {

    return "pass";

    } else {

    return "fail";

    }

    }

    }

    • 3.整合spring

    <?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:aop="http://www.springframework.org/schema/aop"

    xmlns:tx="http://www.springframework.org/schema/tx" 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-3.0.xsd    

              http://www.springframework.org/schema/tx    

             http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   

             http://www.springframework.org/schema/context    

              http://www.springframework.org/schema/context/spring-context-3.0.xsd    

             http://www.springframework.org/schema/aop    

              http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

              http://cxf.apache.org/jaxws

              http://cxf.apache.org/schemas/jaxws.xsd

              ">

    <bean id="propertyConfigurer"

    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

    <property name="locations">

    <list>

    <value>/WEB-INF/jdbc.properties</value>

    </list>

    </property>

    </bean>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

    <property name="driverClass" value="\${jdbc.driverClassName}" />

    <property name="jdbcUrl" value="\${jdbc.url}" />

    <property name="user" value="\${jdbc.username}" />

    <property name="password" value="\${jdbc.password}" />

    <property name="autoCommitOnClose" value="true" />

    <property name="checkoutTimeout" value="\${cpool.checkoutTimeout}" />

    <property name="initialPoolSize" value="\${cpool.minPoolSize}" />

    <property name="minPoolSize" value="\${cpool.minPoolSize}" />

    <property name="maxPoolSize" value="\${cpool.maxPoolSize}" />

    <property name="maxIdleTime" value="\${cpool.maxIdleTime}" />

    <property name="acquireIncrement" value="\${cpool.acquireIncrement}" />

    <property name="maxIdleTimeExcessConnections" value="\${cpool.maxIdleTimeExcessConnections}" />

    </bean>

    <bean id="sessionFactory"

    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

    <property name="dataSource" ref="dataSource" />

    <property name="mappingLocations">

    <list>

    <value>classpath:/test/*.hbm.xml</value>

    </list>

    </property>

    <property name="hibernateProperties">

    <value>

    hibernate.dialect=org.hibernate.dialect.Oracle9Dialect

    hibernate.show_sql=true

    hibernate.format_sql=false

    hibernate.query.substitutions=true 1, false 0

    hibernate.jdbc.batch_size=20

    hibernate.cache.use_query_cache=true

    hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider

    hibernate.hbm2ddl.auto = create

    </value>

    </property>

    </bean>

    <bean id="transactionManager"

    class="org.springframework.orm.hibernate3.HibernateTransactionManager">

    <property name="sessionFactory" ref="sessionFactory" />

    <property name="dataSource" ref="dataSource" />

    </bean>

    <!-- 事务管理器 -->

    <tx:advice id="TestAdvice" transaction-manager="transactionManager">

    <tx:attributes>

    <tx:method name="save*" propagation="REQUIRED" />

    <tx:method name="del*" propagation="REQUIRED" />

    <tx:method name="update*" propagation="REQUIRED" />

    <tx:method name="add*" propagation="REQUIRED" />

    <tx:method name="find*" propagation="REQUIRED" />

    <tx:method name="get*" propagation="REQUIRED" />

    <tx:method name="apply*" propagation="REQUIRED" />

    </tx:attributes>

    </tx:advice>

    <!-- 配置参与事务的类 -->

    <aop:config>

    <aop:pointcut id="allTestServiceMethod"

    expression="execution(* com.lcell.service.impl.*.*(..))" />

    <aop:advisor pointcut-ref="allTestServiceMethod"

    advice-ref="TestAdvice" />

    </aop:config>

    <!-- 事务管理结束 -->

    <jaxws:endpoint id="hello"

    implementor="com.webservice.endpoint.impl.HelloServiceImpl" address="/Hello" />

    </beans>    

    • 4.启动服务,查看wsdl

      

    • 5.编写客户端代码

    我们可以有多种方式来实现,一是拷贝服务器端的基础代码,然后再手动编写调用接口代码,这个的好处是代码量少,清晰可控,二是用wsdl2java自动生成,方便但代码量比较多。为了方便直接采用ant工具构建:

     

    <?xml version="1.0"?>

    <project name="cxf wsdl2java" basedir=".">  

       <property name="cxf.home" location ="\${basedir}/WebRoot/WEB-INF/"/>

       <path id="cxf.classpath">

          <fileset dir="\${cxf.home}/lib">

             <include name="*.jar"/>

          </fileset>

       </path>     

       <target name="cxfWSDLToJava">

          <java classname="org.apache.cxf.tools.wsdlto.WSDLToJava" fork="true">

             <arg value="-client"/>

             <arg value="-d"/>

             <arg value="src"/>

             <arg value="http://127.0.0.1/lcellOnto/Hello?wsdl"/>

             <classpath>

                <path refid="cxf.classpath"/>

             </classpath>

          </java>

       </target>

    </project>

    • 6.运行测试

    运行Hello_HelloServiceImplPort_Client.java,查看结果。。。

     

    至此一个非常简单的基于cxf和spring的的web service做好了,当然这只是基于soap的,还有rest的webservice可以实现。以后学习元可以往web servcie方向走,形成各自独立的模块,通过webservice调用。

    • 标签:
    • schema
    • tx
    • string
    • method
    • property
    • http
    • webservice
    • id
    • hello
    • value
    • www.springframework.org
  • 加入的知识群:
    学习元评论 (0条)

    评论为空
    聪明如你,不妨在这 发表你的看法与心得 ~



    登录之后可以发表学习元评论
      
暂无内容~~
顶部