最近项目中有用到Webservice,研究了Spring与CXF整合的官方文档(其实官方文档说的清楚了,建议大伙还是看官方文档,都是很简单英文单词),所以就有了下面的demo,相信大伙都能看懂
用的主要架构为Maven + Spring + CXF + IDEA,废话就不多说了,先看下整个个项目结构
一、项目结构图
二、服务端
1.我们首先来编写服务端接口HelloWorld.java
package cn.starlin.springcxf;import javax.jws.WebService;/** * 定义服务接口 * on 2016/03/01 9:34. */@WebServicepublic interface HelloWorld { public String sayHello(String str);}
2.定义服务端接口实现类HelloWorldImpl
package cn.starlin.springcxf;import javax.jws.WebService;/** * 服务接口实现类 * on 2016/03/01 9:36. */@WebService(endpointInterface = "cn.starlin.springcxf.HelloWorld")public class HelloWorldImpl implements HelloWorld { public String sayHello(String str) { System.out.println("syaHello called"); return "Hello " + str; }}
3.声明servers的bean,创建cxf-servlet.
<beans ="http://www.springframework.org/schema/beans" ="http://www.w3.org/2001/ ="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!--由Spring管理--> <bean id="hello" class="cn.starlin.springcxf.HelloWorldImpl" /> <jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" /></beans>
4.配置web.
<web-app ="http://java.sun.com/ ="http://www.w3.org/2001/ version="2.5" xsi:schemaLocation="http://java.sun.com/> <display-name>cxf</display-name> <servlet> <description>Apache CXF Endpoint</description> <display-name>cxf</display-name> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <session-config> <session-timeout>60</session-timeout> </session-config></web-app>
5.启动Tomcat,访问http://localhost:8080/SpringCXF/,若能出现下图界面,就表示服务端成功了
三、客户端
1.客户端 client-beans.
<??><beans ="http://www.springframework.org/schema/beans" ="http://www.w3.org/2001/ ="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd"> <bean id="client" class="cn.starlin.springcxf.HelloWorld" factory-bean="clientFactory" factory-method="create"/> <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> <property name="serviceClass" value="cn.starlin.springcxf.HelloWorld"/> <property name="address" value="http://localhost:8080/SpringCXF/HelloWorld"/> </bean></beans>
2.客户端代码HelloWorldClien.java
package cn.starlin.springcxf;import org.springframework.context.support.ClassPath/** * 客户端 * on 2016/03/01 10:53. */public class HelloWorldClient { public static void main(String args[]) throws Exception { ClassPath= new ClassPathnew String[] {"client-beans.}); HelloWorld client = (HelloWorld)context.getBean("client");//client需与client-beans. String response = client.sayHello("starlin"); System.out.println("Response: " + response); System.exit(0); }}
3.运行以下客户端代码,出现以下信息就表示成功了
同时服务端也会打印出相关信息
至此整合完成!!!!
原标题:Spring与CXF
关键词:Spring