CXF 是Apache旗下一款非常优秀的Web Services 开源框架,具备轻量级的特性, 而且能无缝的整合到spring中
CXF是Celtix与XFire的整合, 前者是一款ESB框架, 后者是Web Services框架,下面我们来重点介绍一下CXF与Spring的集成
第一步: 配置maven依赖:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>spring-cxf</name> <groupId>com</groupId> <artifactId>spring-cxf</artifactId> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>4.0.5.RELEASE</spring.version> <cxf.version>3.0.0</cxf.version> <jackson.version>2.4.1</jackson.version> </properties> <dependencies> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <!-- CXF --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxrs</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> </dependency> <!--jackson--> <dependency> <groupId>com.fasterxml.jackson.jaxrs</groupId> <artifactId>jackson-jaxrs-json-provider</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-rs-extension-providers</artifactId> <version>${cxf.version}</version> </dependency> </dependencies> </project>
第二步: 写一个WS接口及其实现:
@WebService public interface HelloService { String say(String name); }
实现接口:
@WebService @Component public class HelloServiceImpl implements HelloService { public String say(String name) { return "hello " + name; } }
在实现类上加上@Component注解, 这样才能被Spring IOC容器扫描到,认为它是一个spring bean, 可以根据Bean ID来获取实例
第三步: 配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app 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_3_0.xsd" version="3.0"> <!-- Spring --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- CXF --> <servlet> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping> </web-app>
所以带有/ws前缀的请求, 都会被CXFServlet进行处理
第四步:配置spring
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:component-scan base-package="com.*"/> <import resource="spring-cxf.xml"/> </beans>
第五步: 配置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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!--HelloService--> <jaxws:server id="helloService" address="/soap/hello"> <jaxws:serviceBean> <ref bean="helloServiceImpl"/> </jaxws:serviceBean> </jaxws:server> <bean id="helloServiceImpl" class="com.HelloServiceImpl"/> </beans>
通过CXF提供的spring 命名空间,即jaxws:server 来发布ws, address请求的路径信息
第六步:启动tomcat
将应用部署到tomcat中, 在浏览器中输入以下地址可进入CXF控制台:
http://localhost:8080/spring-cxf/ws
通过http://localhost:8080/spring-cxf/ws/soap/hello?wsdl 可以查看WSDL
现在已经成功的通过CXF对外发布Web Services, 下面介绍一下如何通过客户端调用这些endpoint
关于客户端调用,这里我们主要介绍动态代理的方式:
public class DynamicClient { public static final String END_POINT = "http://localhost:8080/spring-cxf/ws/soap/hello?wsdl"; public static void main(String[] args) { DynamicClientFactory factory = DynamicClientFactory.newInstance(); Client client = factory.createClient(END_POINT); try { Object[] results = client.invoke("say", "world"); System.out.println(results[0]); } catch (Exception e) { e.printStackTrace(); } } }
通过前面的介绍,我们大致了解到Spring + CXF 的集成让发布WS更加简单, 具体可分为四个步骤:
配置web.xml
编写WS接口及实现
配置CXF的endpoint
启动web 容器
接下来我们将介绍一下使用CXF开发REST服务