cxf3.x +spring 3.x(4.x)+ maven 發布webservice 服務


cxf 在做企業級webservices 服務的時候確實非常好用,個人覺得比axis1, 2都好用。
雖然spring自身也提供了webservices發布方法,這里使用cxf跟spring結合,使用起來非常方便;


整體項目結果如下:

整體結構

基於maven 來配置使得項目更加簡單

  1. 新建一個maven 項目,補全 src下的main和test目錄

  2. 打開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_2_5.xsd" id="WebApp_ID" version="2.5"> 
<display-name>cxf</display-name> 
<servlet> 
<description>m CXF Endpoint</description> 
<display-name>cxf</display-name> 
<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> 
</web-app>
  1. 新建一個cxf-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> 
<!-- START SNIPPET: beans --> 
<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.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-servlet.xml"/> 
<jaxws:endpoint id="DepartmentService" implementor="com.qycloud.oatos.ty.department.DepartmentServiceImpl" address="/DepartmentService"/> 
<jaxws:endpoint id="UserService" implementor="com.qycloud.oatos.ty.user.UserServiceImpl" address="/UserService"/> 
</beans>
  1. 補全 pom.xml ,主要是spring 的依賴和cxf,cxf 只需要以下2個
<dependency> 
<groupId>org.apache.cxf</groupId> 
<artifactId>cxf-rt-frontend-jaxws</artifactId> 
<version>3.0.0</version> 
</dependency> 
<dependency> 
<groupId>org.apache.cxf</groupId> 
<artifactId>cxf-rt-transports-http</artifactId> 
<version>3.0.0</version> 
</dependency>
  1. 編寫代碼:這里我們回頭看到cxf-servlet.xml 里面可發現我們只需要制定一個id,一個是實現類,一個訪問地址即可。

a) 編寫一個接口

@WebService 
public interface DepartmentService { 

boolean updateTheOrg(String org); 

boolean updateOrgsCode(String org); 

}

b) 編寫一個實現類

@WebService(endpointInterface = "com.qycloud.oatos.ty.department.DepartmentService") 
public class DepartmentServiceImpl implements DepartmentService { 

@Override 
public boolean updateTheOrg(String org) { 
// TODO Auto-generated method stub 
return false; 
} 

@Override 
public boolean updateOrgsCode(String org) { 
// TODO Auto-generated method stub 
return false; 
} 

}

7.剩下的事情就是發布了。右鍵run on server。結果如下

效果

  1. demo下載地址


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM