CXF整合Spring发布WebService实例

来源:互联网 发布:淘宝刷客兼职 编辑:程序博客网 时间:2024/06/10 13:47

① 新建 Java Dynamic Web project 工程 ,导入CXF与spring的相关jar包,具体用到的包如下:

aopalliance-1.0.jarasm-5.0.4.jarcxf-core-3.1.4.jarcxf-rt-bindings-soap-3.1.4.jarcxf-rt-bindings-xml-3.1.4.jarcxf-rt-databinding-jaxb-3.1.4.jarcxf-rt-frontend-jaxws-3.1.4.jarcxf-rt-frontend-simple-3.1.4.jarcxf-rt-transports-http-3.1.4.jarcxf-rt-ws-addr-3.1.4.jarcxf-rt-ws-policy-3.1.4.jarcxf-rt-wsdl-3.1.4.jarjaxb-core-2.2.11.jarjaxb-impl-2.2.11.jarjaxb-xjc-2.2.11.jar jcl-over-slf4j-1.7.12.jarneethi-3.0.3.jarslf4j-api-1.7.12.jarslf4j-jdk14-1.7.12.jarspring-aop-4.1.7.RELEASE.jarspring-beans-4.1.7.RELEASE.jarspring-context-4.1.7.RELEASE.jarspring-core-4.1.7.RELEASE.jarspring-expression-4.1.7.RELEASE.jarspring-web-4.1.7.RELEASE.jarstax2-api-3.1.4.jarwoodstox-core-asl-4.4.1.jarwsdl4j-1.6.3.jarxml-resolver-1.2.jarxmlschema-core-2.2.1.jar
② 在工程的web.xml文件中增加支持spring与CXF的配置:
  <servlet>    <servlet-name>CXFService</servlet-name>    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class></servlet> <servlet-mapping>    <servlet-name>CXFService</servlet-name>    <url-pattern>/*</url-pattern></servlet-mapping>

③ 新建com.htxx.cxf.service包 ,并增加提供webservice服务的接口类以及其实现:
package com.htxx.cxf.service;import javax.jws.WebParam;import javax.jws.WebService;import com.htxx.entity.business.User;@WebServicepublic interface HelloService {public String sayHello(@WebParam(name="text")String text);public User getUserByName(String name);}

package com.htxx.cxf.service;import com.htxx.entity.business.User;public class HelloServiceImpl implements HelloService {public String sayHello(String text) {// TODO Auto-generated method stubreturn "hello"+text;}public User getUserByName(String name) {// TODO Auto-generated method stubUser user=new User();user.setName(name);user.setPwd("123");return user;}}

public class User {    private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}public User(){}public User(String name) {super();this.name = name;}    @Overridepublic String toString() {return "User [name=" + name + "]";}}

④ . 配置CXF配置文件来发布webservice服务:
新建xml配置文件 :cxf.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"xmlns:soap="http://cxf.apache.org/bindings/soap"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsdhttp://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd">     <!--         1.使用jaxws:endpoint标签的配置发布一个 webservice         2.服务提供实现类为 com.elgin.cxf.service.impl.HelloServiceImpl,用implementor属性配置        3.address属性配置外部访问的相对路径        4.使用  jaxws:inInterceptors 标签配置2个日志拦截器,用来打印调用时的日志信息        5.注意:在此配置文件中,需加入jaxws与soap命名空间   -->   <jaxws:endpoint id="helloService"                    implementor="com.htxx.cxf.service.HelloServiceImpl"                    address="/hello" >         <jaxws:inInterceptors >   <bean id="inMessageInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"/>     <bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>          </jaxws:inInterceptors>   </jaxws:endpoint>   </beans>
⑤ . 在src 下新建spring的配置文件 :applicationContext.xml ,并导入CXF的配置到此文件中:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">        <!-- CXF服务端发布配置 -->   <import resource="/cxf.xml"/>      <!-- 项目中其它bean配置 -->   <!--         ....    --></beans>

⑥ . 测试发布情况:

经过以上5个步骤 ,CXF的相关配置完成,将项目加载到Tomcat ,并启动 ,访问 如下URL:http://localhost:8090/CXF/hello?wsdl ,出现下面的xml数据,说明发布成功:



⑦写一个client测试:
package com.htxx.cxf.client;import javax.xml.namespace.QName;import org.apache.cxf.endpoint.Client;import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;import com.htxx.cxf.service.HelloService;import com.htxx.entity.business.User;public class ClientDynamic {public static void main(String[] args) {        method1();}public static void method1(){    //调用WebService    JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();    factory.setServiceClass(HelloService.class);    factory.setAddress("http://localhost:8090/CXF/hello");        HelloService service = (HelloService) factory.create();       // System.out.println("#############Client getUserByName##############");    String hello=service.sayHello("sasa");    System.out.println(hello);    User user=service.getUserByName("qqq");    System.out.println(user.toString());}}

输出结果:
hellosasaUser [name=qqq, pwd=123]
-----------------------------------------------------参考----------------------------------------------
http://www.openvp.cn/spring/50621370724709183451.html
http://www.cnblogs.com/hoojo/archive/2011/03/30/1999563.html