CXF和spring整合实现webservice实例
环境:JDK6.0,cxf-2.0.7,spring2.5,tomcat6.0
服务器端:
1.新建Web项目,例如webws,导入cxf-2.0.7/lib下的jar包和spring2.5的包,因为cxf支持spring,因此它自带有sping的一些核心包,为了以后扩展,保险起见都一起导入吧。。
2.新建一个服务接口IHelloWorld.java,该接口是面向客户端,暴露服务:
package cn.com.service;import javax.jws.WebParam;import javax.jws.WebService;@WebServicepublic interface IHelloWorld { //@WebParam(name="arg0")可有可无,为了增强可读性public String sayHello(@WebParam(name="arg0")String text);}package cn.com.service;import javax.jws.WebService;@WebService(endpointInterface="cn.com.service.IHelloWorld")public class HelloWorldImpl implements IHelloWorld {public String sayHello(String text) {return "Hello" + text ;}}<?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:p="http://www.springframework.org/schema/p"xmlns:jaxws="http://cxf.apache.org/jaxws"xmlns:cxf="http://cxf.apache.org/core"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"><import resource="classpath:META-INF/cxf/cxf.xml" /><import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /><import resource="classpath:META-INF/cxf/cxf-servlet.xml" /><bean id="hello" implementor="#hello" address="/HelloWorld" /></beans>
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><servlet-name>CXFServlet</servlet-name><display-name>CXF Servlet</display-name><servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>CXFServlet</servlet-name><url-pattern>/*</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>
package pro.webws.client;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-client.xml");IHelloWorld client = (IHelloWorld) ctx.getBean("client");String result = client.sayHello("你好!");System.out.println(result);}}<?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:p="http://www.springframework.org/schema/p"xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd"><bean id="client" factory-bean="clientFactory"factory-method="create" /><bean id="clientFactory" value="pro.webws.client.IHelloWorld" /><property name="address" value="http://localhost:8080/webws/HelloWorld" /></bean></beans>