在前一篇的博客中,我使用Xfire1.x來開發了WebServies的服務端。java
可是若是你訪問Apache的官網,能夠看到xfire已經被合併了。web
最新的框架叫作CXF。
Apache CXF = Celtix + XFire。
CXF 繼承了 Celtix 和 XFire 兩大開源項目的精華,
提供了對 JAX-WS 全面的支持,而且提供了多種 Binding 、DataBinding、Transport 以及各類 Format 的支持,而且能夠根據實際項目的須要,採用代碼優先(Code First)或者 WSDL 優先(WSDL First)來輕鬆地實現 Web Services 的發佈和使用。spring
總之,就是cxf就是好啦。那麼接下來咱們使用cxf來開發一個webServices的服務端。體驗CXF的好處。apache
環境:MyEclipse6.01+jdk5session
1)首先,建一個web工程.app
2)寫好一個接口和服務類框架
//服務接口IHelloService.javaide
package com.pengzj.service;工具
import java.util.List;網站
import javax.jws.WebMethod;
import javax.jws.WebService;
/**
*
* @author atixujie#gz-accp
*
*/
@WebService
public interface IHelloService {
@WebMethod
public String sayHi(String uname);
}
//webServices的實現類IHelloServiceImpl .java
import com.pengzj.service.IHelloService;
@WebService
public class IHelloServiceImpl implements IHelloService{
@Override
@WebMethod
public String sayHi(String uname) {
return "Hello "+uname;
}
}
你們能夠注意到,這裏用到了webServices的註解。@WebService和@WebMethod.
3)導入CXF的jar包。
到apache的網站上下載CXF的包。
最新的版本是2.2.9 。固然若是你下載這個包就會比較麻煩。由於它須要最新的JDk(1.6.01都不行。要什麼1.6.u11)的支持。
因此建議你仍是下載2.0.4地址以下:
http://people.apache.org/dist/incubator/cxf/2.0.4-incubator/apache-cxf-2.0.4-incubator.zip
解壓以後將lib文件夾下的jar包都複製到項目中。
4)配置CXF。
而後將web.xml的配置文件改爲以下:
<display-name>cxf</display-name>
<description>cxf</description>
<servlet>
<description>Apache CXF Endpoint</description>
<servlet-name>cxf</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
5)配置Spring的配置文件。
由於CXF集成了Spring。因此以上的配置要默認到WEB-INF/下找spring的配置文件applicationContext.xml。
因此咱們要在WEB-INF下創建一個Spring的配置文件applicationContext.xml以下:
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.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"/>
<!-- 配置好webservices的類名和服務名 -->
<jaxws:endpoint id="hellows"
implementor="com.pengzj.service.impl.IHelloServiceImpl" address="/Hellows" />
</beans>
準備工做結束了。
部署,運行。在地址上輸入:http://localhost:8080/cxfws_0619/services/
就應該能夠看到一個超連接,點擊能夠看到以下的wsdl-xml文件
<?xml version="1.0" encoding="utf-8" ?>
+ <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://service.pengzj.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://impl.service.pengzj.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="IHelloServiceImplService" targetNamespace="http://impl.service.pengzj.com/">
ok.使用CXF開發服務端大功告成。
總結步驟:
1)創建web工程。導入cxf的jar包。
2)配置web.xml文件
3)配置spring的配置文件。同時配置好服務類的bean.
4)部署運行。
在下一篇中,咱們將介紹利用wsdl2java工具生成代碼,完成客戶端的調用。