cxf整合Spring實現(xiàn)webservice調用實踐
cxf整合Spring實現(xiàn)webservice
server端
pom文件內容
<dependencies>
<!-- CXF WS開發(fā) -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
<!-- 運行tomcat7方法:tomcat7:run -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<!-- 指定端口 -->
<port>8080</port>
<!-- 請求路徑 -->
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>web.xml文件里的內容
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--webservice服務端,發(fā)布服務需要配置CXFSrvlet-->
<!--這里配置的servlet路徑,為最終服務路徑的一部分-->
<!--服務訪問路徑:http://localhost:8080/web.xml配置路徑/spring配置的路
徑 -->
<servlet>
<servlet-name>cxfServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cxfServlet</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
</web-app>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:cxf="http://cxf.apache.org/core"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/core
http://cxf.apache.org/schemas/core.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">
<!--
1. 服務地址
2. 服務bean
完整服務地址:
http://localhost:8080/ws/userService
Spring整合ApacheCXF發(fā)布jaxws服務
-->
<jaxws:server address="/userService">
<jaxws:serviceBean>
<bean class="com.sks.service.impl.UserServiceImpl"/>
</jaxws:serviceBean>
</jaxws:server>
</beans>接口
@WebService
public interface UserService {
public String sayHello(String name, int age);
}
接口實現(xiàn)類
public class UserServiceImpl implements UserService {
@Override
public String sayHello(String name, int age) {
return "hello " + name + " " + age;
}
}
最后點擊如圖所示的按鈕運行項目。

在瀏覽器中輸入http://localhost:8080/ws/userService?wsdl,訪問wsdl說明書,可以看到以下內容:

client端
項目目錄結構,web.xml文件中的內容不需要改動

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:cxf="http://cxf.apache.org/core"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/core
http://cxf.apache.org/schemas/core.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">
<!--Spring整合ApacheCXF,客戶端配置
關鍵點:
通過Spring整合ApacheCXF,創(chuàng)建客戶端的代理對象,遠程訪問服務端。
jaxws:client
id 應用中注入的接口的代理對象的名稱
address 服務端訪問地址
serviceClass 指定接口路徑,會根據(jù)該類型生成代理對象
-->
<jaxws:client id="userService" address="http://localhost:8080/ws/userService" serviceClass="com.sks.service.UserService">
</jaxws:client>
</beans>測試類
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Test1 {
@Resource
private UserService userService;
@Test
public void test1() {
System.out.println(userService);
System.out.println(userService.getClass());
//遠程服務調用接口
String context = userService.sayHello("球球",19);
System.out.println(context);
}
}
點擊運行以后控制臺打印輸出結果如下,這說明我們的調用成功了。

總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Spring Security動態(tài)權限的實現(xiàn)方法詳解
這篇文章主要和小伙伴們簡單介紹下 Spring Security 中的動態(tài)權限方案,以便于小伙伴們更好的理解 TienChin 項目中的權限方案,感興趣的可以了解一下2022-06-06
SpringBoot+MyBatis-Plus實現(xiàn)分頁功能
在SpringBoot項目中,結合MyBatis-Plus(簡稱MP)可以非常方便地實現(xiàn)分頁功能,MP為開發(fā)者提供了分頁插件PaginationInterceptor,只需簡單配置即可使用,本文給大家介紹了SpringBoot+MyBatis-Plus實現(xiàn)分頁功能,文中通過代碼示例給大家介紹的非常詳細,需要的朋友可以參考下2024-01-01
System.currentTimeMillis()計算方式與時間的單位轉換詳解
這篇文章主要介紹了System.currentTimeMillis()計算方式與時間的單位轉換詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-05-05
IntelliJ IDEA安裝scala插件并創(chuàng)建scala工程的步驟詳細教程
這篇文章主要介紹了IntelliJ IDEA安裝scala插件并創(chuàng)建scala工程的步驟,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07

