最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

cxf整合Spring實現(xiàn)webservice調用實踐

 更新時間:2026年04月27日 14:37:44   作者:wmxz520  
文章介紹了使用CXF整合Spring實現(xiàn)WebService的過程,包括pom文件配置、web.xml和applicationContext.xml文件配置、接口定義與實現(xiàn)、項目運行及測試,通過訪問wsdl地址驗證服務可用性,最后通過客戶端測試類驗證調用成功

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)權限的實現(xiàn)方法詳解

    這篇文章主要和小伙伴們簡單介紹下 Spring Security 中的動態(tài)權限方案,以便于小伙伴們更好的理解 TienChin 項目中的權限方案,感興趣的可以了解一下
    2022-06-06
  • Java實時信號處理的五種方式

    Java實時信號處理的五種方式

    實時信號處理還在卡頓?這和用老式打字機寫小說有什么區(qū)別,Java與實時信號處理系統(tǒng)的集成就像給系統(tǒng)裝了個"魔法加速器",今天我們就用Java代碼+實戰(zhàn)技巧,打造一套"實時信號處理的魔法工具箱",讓你的系統(tǒng)比"咖啡機出水"還流暢,需要的朋友可以參考下
    2025-06-06
  • SpringBoot+MyBatis-Plus實現(xiàn)分頁功能

    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()計算方式與時間的單位轉換詳解

    這篇文章主要介紹了System.currentTimeMillis()計算方式與時間的單位轉換詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-05-05
  • Java遞歸方法求5!的實現(xiàn)代碼

    Java遞歸方法求5!的實現(xiàn)代碼

    這篇文章主要介紹了Java遞歸方法求5!的實現(xiàn)代碼,需要的朋友可以參考下
    2017-02-02
  • 基于Java實現(xiàn)一個高效可伸縮的計算結果緩存

    基于Java實現(xiàn)一個高效可伸縮的計算結果緩存

    這篇文章將通過對一個計算結果緩存的設計迭代介紹,分析每個版本的并發(fā)缺陷,并分析如何修復這些缺陷,最終完成一個高效可伸縮的計算結果緩存,感興趣的小伙伴可以了解一下
    2023-06-06
  • 淺談java中文本框和文本區(qū)

    淺談java中文本框和文本區(qū)

    本文給大家介紹的是java中的文本框和文本區(qū)的概念和使用方法,以及簡單的示例,十分實用,有需要的小伙伴可以參考下。
    2015-06-06
  • SpringMVC的源碼解析

    SpringMVC的源碼解析

    本文主要介紹了SpringMVC的源碼解析。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-02-02
  • IntelliJ IDEA安裝scala插件并創(chuàng)建scala工程的步驟詳細教程

    IntelliJ IDEA安裝scala插件并創(chuàng)建scala工程的步驟詳細教程

    這篇文章主要介紹了IntelliJ IDEA安裝scala插件并創(chuàng)建scala工程的步驟,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07
  • 詳解Java修飾符

    詳解Java修飾符

    Java語言提供了很多修飾符,主要分為以下兩類:訪問修飾符;非訪問修飾符。修飾符用來定義類、方法或者變量,通常放在語句的最前端。我們通過下面的例子來說明,下面就跟小編一起來看下吧
    2016-12-12

最新評論

堆龙德庆县| 筠连县| 石泉县| 遂昌县| 定结县| 乌审旗| 芦溪县| 凤城市| 许昌县| 许昌市| 湟中县| 翼城县| 康保县| 普兰店市| 紫金县| 弥勒县| 襄樊市| 汾阳市| 麦盖提县| 金川县| 盘山县| 栾川县| 彰武县| 登封市| 左云县| 吉水县| 同仁县| 岑溪市| 高邑县| 尤溪县| 防城港市| 墨竹工卡县| 营口市| 靖边县| 明光市| 察隅县| 许昌市| 台前县| 奉化市| 阳城县| 黔东|