基於Spring和CXF的webservice開發環境搭建
阿新 • • 發佈:2019-02-11
使用CXF釋出webservice服務時,規範的做法是先書寫一個介面,用以宣告服務型別。
基於Spring和CXF開發web service的框架搭建
一、建立web專案
Eclipse中新建一個dynamic webproject,命名為:CXFTest
二、匯入需要的jar包
把下載的CXF專案的解壓縮檔案中lib資料夾下的所有jar包拷貝到WebContent->WEB-INF->lib資料夾下
三、建立服務介面
在Java resource->src目錄下新建package包:com.cxfspring.service,並在該包中新建服務介面Ipeople,Ipeople介面程式碼如下:
package com.cxfspring.service;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface IPeople
{
@WebMethod
public String sayHello(String name);
}
四、建立服務實現類
在Java resource->src目錄下新建package包:com.cxfspring.serviceImpl,並在該包中新建服務實現類Student,Student實現Ipeople介面,Student類程式碼如下:
package com.cxfspring.serviceImpl; import javax.jws.WebMethod; import javax.jws.WebService; import com.cxfspring.service.IPeople; @WebService public class Student implements IPeople { @WebMethod @Override publicString sayHello(String name) { //TODO Auto-generated method stub System.out.println("Hello:"+name); return"Hello:"+name+"nice to meet you!"; } }
五、配置web.xml檔案
在WebContent->WEB-INF目錄下新建web.xml,配置程式碼如下:
<?xml version="1.0"encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>ServiceManagement</display-name>
<!-- 載入Spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置CXF的核心servlet -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<!-- <init-param>
配置Spring的配置檔案
<param-name>config-location</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
六、配置Spring配置檔案
在WebContent->WEB-INF目錄下新建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:tx="http://www.springframework.org/schema/tx"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:jms="http://cxf.apache.org/transport/jms"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/tx/spring-aop.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/context/spring-context.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/transport/jms
http://cxf.apache.org/schemas/configuration/jms">
<!-- 匯入CXF為擴充套件Spring提供的幾個XML配置檔案 -->
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<import resource="classpath:META-INF/cxf/cxf-extension-object-binding.xml"/>
<!-- 利用endpoint釋出服務 -->
<jaxws:endpoint id="HelloService"implementor="com.cxfspring.serviceImpl.Student" address="/HelloService"/>
</beans>
七、驗證服務是否能釋出
把工程部署到tomcat上,並啟動tomcat,在瀏覽器中輸入http://localhost:8090/CXFTest/HelloService?wsdl 。如果瀏覽器能顯示該服務的WSDL文件,則說明該服務能成功釋出。