本文段摘录自 http://www.ibm.com/developerworks/cn/webservices/ws-pojo-springcxf/index.html
Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构。它容许建立高性能和可扩展的服务,您能够将这样的服务部署在 Tomcat 和基于 Spring 的轻量级容器中,以及部署在更高级的服务器上,例如 Jboss、IBM® WebSphere® 或 BEA WebLogic。html
功能前端
该框架提供了如下功能:java
Web 服务标准支持:CXF 支持如下 Web 服务标准:
Java API for XML Web Services (JAX-WS)
SOAP
Web 服务描述语言(Web Services Description Language ,WSDL)
消息传输优化机制(Message Transmission Optimization Mechanism,MTOM)
WS-Basic Profile
WS-Addressing
WS-Policy
WS-ReliableMessaging
WS-Security前端建模 :CXF 提供了前端建模的概念,容许您使用不一样的前端 API 来建立 Web 服务。API 容许您使用简单的工厂 Bean 并经过 JAX-WAS 实现来建立 Web 服务。它还容许您建立动态 Web 服务客户端.
Eclipse
Maven
Tomcat 8.0web
测试软件 SoapUI 5.2.1spring
下载Apache CXF 压缩包apache
构建Maven项目(使用archetype,项目名称为 cxf_my_example)服务器
org.apache.cxf.archetype架构
cxf-jaxws-javafirst 3.1.4app
将项目发布到Tomcat服务器 端口 8080 运行服务器框架
访问wsdl地址 http://localhost:8080/cxf_my_example/HelloWorld?wsdl
响应wsdl文件表示服务发布成功
项目主要引用包(Maven自动配置其它包):
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.1.4</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.1.4</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.1.7.RELEASE</version> </dependency>
定义接口:
@WebService注解只是标注该接口为Web服务的访问接口,@WebParam标注参数名称,便于生成友好的WSDL.
@WebService public interface HelloWorld { String sayHi(@WebParam(name="text")String text); }
定义实现:
实现类也须要标注@WebService,并设置参数endpointInterface指定实现的接口的全限定名称
@WebService(endpointInterface = "my.zhwc.cxf_my_example.HelloWorld") public class HelloWorldImpl implements HelloWorld { public String sayHi(String text) { return "Hello " + text; } }
SpringBean 配置:
如今已经建立了一个接口和实现,使用 CXF,能够使用 JAX-WS 前端使其成为实际的服务组件.
<import resource="classpath:META-INF/cxf/cxf.xml" /> <jaxws:endpoint id="helloWorld" implementor="my.zhwc.cxf_my_example.HelloWorldImpl" address="/HelloWorld" />
Web.XML配置:
如今,使用Spring加载配置文件,注册CXF Servlet 处理全部客户端的访问请求.
<web-app> <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/beans.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> </web-app>
打开SoapUI,新建一个空项目,添加wsdl地址[http://localhost:8080/cxf_my_example/HelloWorld?wsdl]
在请求中填入参数 cong,返回结果 Hello cong ,以下:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cxf="http://cxf_my_example.zhwc.my/"> <soapenv:Header/> <soapenv:Body> <cxf:sayHi> <!--Optional:--> <text>cong</text> </cxf:sayHi> </soapenv:Body> </soapenv:Envelope>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <ns2:sayHiResponse xmlns:ns2="http://cxf_my_example.zhwc.my/"> <return>Hello cong</return> </ns2:sayHiResponse> </soap:Body> </soap:Envelope>
咱们直接在服务端工程中编写客户端代码,这样无需使用CXF 的 wsdl2java工具生成客户端接口代码了.
<?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" 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"> <bean id="client" class="my.zhwc.cxf_my_example.HelloWorld" factory-bean="clientFactory" factory-method="create" /> <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> <property name="serviceClass" value="my.zhwc.cxf_my_example.HelloWorld" /> <property name="address" value="http://localhost:8080/cxf_my_example/HelloWorld" /> </bean> </beans>
import my.zhwc.cxf_my_example.HelloWorld; public class Client { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"classpath:my/zhwc/cxf_my_example/client/client-beans.xml"}); HelloWorld client = (HelloWorld)context.getBean("client"); System.out.println(client.sayHi("Cong")); System.exit(0); } }
在服务端已发布的状态下,运行Client.java 可得输出 Hello Cong
http://www.ibm.com/developerworks/cn/webservices/ws-pojo-springcxf/index.html