① 新建 Java Dynamic Web project 工程 ,导入CXF与spring的相关jar包,具体用到的包如下:
② 在工程的web.xml文件中增加支持spring与CXF的配置:aopalliance-1.0.jar asm-5.0.4.jar cxf-core-3.1.4.jar cxf-rt-bindings-soap-3.1.4.jar cxf-rt-bindings-xml-3.1.4.jar cxf-rt-databinding-jaxb-3.1.4.jar cxf-rt-frontend-jaxws-3.1.4.jar cxf-rt-frontend-simple-3.1.4.jar cxf-rt-transports-http-3.1.4.jar cxf-rt-ws-addr-3.1.4.jar cxf-rt-ws-policy-3.1.4.jar cxf-rt-wsdl-3.1.4.jar jaxb-core-2.2.11.jar jaxb-impl-2.2.11.jar jaxb-xjc-2.2.11.jar jcl-over-slf4j-1.7.12.jar neethi-3.0.3.jar slf4j-api-1.7.12.jar slf4j-jdk14-1.7.12.jar spring-aop-4.1.7.RELEASE.jar spring-beans-4.1.7.RELEASE.jar spring-context-4.1.7.RELEASE.jar spring-core-4.1.7.RELEASE.jar spring-expression-4.1.7.RELEASE.jar spring-web-4.1.7.RELEASE.jar stax2-api-3.1.4.jar woodstox-core-asl-4.4.1.jar wsdl4j-1.6.3.jar xml-resolver-1.2.jar xmlschema-core-2.2.1.jar
<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; @WebService public 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 stub return "hello"+text; } public User getUserByName(String name) { // TODO Auto-generated method stub User 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; } @Override public String toString() { return "User [name=" + name + "]"; } }
④ . 配置CXF配置文件来发布webservice服务:
新建xml配置文件 :cxf.xml ,具体配置内容如下:
⑤ . 在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" 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.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://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>
<?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()); } }
输出结果:
-----------------------------------------------------参考----------------------------------------------hellosasa User [name=qqq, pwd=123]
http://www.openvp.cn/spring/50621370724709183451.html
http://www.cnblogs.com/hoojo/archive/2011/03/30/1999563.html