webService之Apache CXF
標籤:webService CXF
一、CXF簡介:
1、Apache CXF是一個開源的webService框架
2、支持多種協議:支持SOAP1.1,SOAP1.2,RESTFUL HTTP,WS-I BasicProfie,WS-Security,等
3、CXF大大簡化了WebService的創建且與Spring進行無縫集成。
二、CXF的編程思路
1、編寫SEI(服務接口)
2、編寫接口實現類
3、配置spring配置文件
4、配置web.xml
5、啓動服務器
三、服務端設計
服務接口
/** * 天氣服務接口 * @author MOTUI * */ @WebService public interface WeatherService { /** * 根據城市名稱獲得天氣情況 * @param cityName * @return */ public String getWeather(String cityName); }
實現類
/** * 服務接口實現類 * @author MOTUI * */ @WebService public class WeatherServiceImpl implements WeatherService { public String getWeather(String cityName) { if ("北京".equals(cityName)) { return "陰轉多雲,18~25攝氏度"; } return "其他地區正在完善"; } }
配置文件(spring 配置文件)
導入命名空間:xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd" ```xml <!-- 配置bean --> <bean id="weatherService" class="com.motui.service.WeatherServiceImpl"/> <!-- 配置webService --> <jaxws:server address="/ws" serviceClass="com.motui.service.WeatherService"> <jaxws:serviceBean> <ref bean="weatherService"/> </jaxws:serviceBean> </jaxws:server> ```
web.xml
<!-- 加載Spring的工廠監聽器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 指定監聽器尋找Spring配置文件所在位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param> <!-- 配置CXF Servlet --> <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>/cxf/*</url-pattern> </servlet-mapping>
啓動服務
部署到Tomcat,啓動服務。 訪問路徑:http://localhost:port/項目名稱/cxf/ws?wsdl進行訪問。 頁面顯示wsdl說明書及部署正確。
四、客戶端設計
生成客戶端代碼
使用命令:wsimport -s . -p 包名 wsdl地址
客戶端代碼(項目爲web項目和java項目都可以):
可以直接調用也可以在客戶端業務層調用,此處在業務層調用接口代碼:
public interface ClientService { public String queryWeather(String cityName); }
實現類代碼:(實現類上添加@service註解)
@Service public class ClientServiceImpl implements ClientService { private WeatherService weatherService; public void setWeatherService(WeatherService weatherService) { this.weatherService = weatherService; } @Override public String queryWeather(String cityName) { String weather = weatherService.getWeather(cityName); return weather; } }
測試類代碼:
/** * 測試客戶端 */ public class TestClient { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); //獲得ClientService ClientService clientService = (ClientService) context.getBean("clientService"); //調用方法 String queryWeather = clientService.queryWeather("北京"); System.out.println(queryWeather); } }
測試結果:
陰轉多雲,18~25攝氏度
五、總結
基於CXF與Spring的webService開發。CXF框架與spring可以進行無縫連接。 在使用CXF時,需要導入CXF的jar包,在導入全部jar在開發中可能會遇到如下問題: 1、配置文件中不自動提示和報紅叉問題。 解決方式:聯網就可以解決。 2、運行過程中報錯:prefix wsdp is not bound to a namespace 由以下四個jar導致,刪除即可,不刪除也不影響正常運行。 cxf-services-ws-discovery-api-3.1.4.jar cxf-services-ws-discovery-service-3.1.4.jar cxf-services-wsn-api-3.1.4.jar cxf-services-wsn-core-3.1.4.jar