CXF 是Apache旗下一款很是優秀的Web Services 開源框架,具有輕量級的特性, 並且能無縫的整合到spring中java
CXF是Celtix與XFire的整合, 前者是一款ESB框架, 後者是Web Services框架,下面咱們來重點介紹一下CXF與Spring的集成web
第一步: 配置maven依賴:spring
<?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接口及其實現:apache
@WebService public interface HelloService { String say(String name); }
實現接口:json
@WebService @Component public class HelloServiceImpl implements HelloService { public String say(String name) { return "hello " + name; } }
在實現類上加上@Component註解, 這樣才能被Spring IOC容器掃描到,認爲它是一個spring bean, 能夠根據Bean ID來獲取實例瀏覽器
第三步: 配置web.xmltomcat
<?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進行處理app
第四步:配置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>
第五步: 配置CXFfrontend
<?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服務