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

Spring Boot 使用 WebServiceTemplate 調(diào)用 WebService的完整步驟

 更新時間:2026年03月30日 11:40:37   作者:高級盤絲洞  
文章介紹了使用Spring WebServiceTemplate調(diào)用WebService的方法,包括添加依賴、配置WebServiceTemplate、編寫調(diào)用工具類、注意事項及完整的調(diào)用步驟,特別強調(diào)了調(diào)用.NET的WebService需要添加SOAPAction請求頭,感興趣的朋友跟隨小編一起看看吧

WebServiceTemplateSpring 官方原生的 SOAP 客戶端工具,無需引入 CXF 等第三方框架,輕量、簡單、和 SpringBoot 無縫整合,非常適合調(diào)用常規(guī)的 WebService(如你截圖的 .NET ASMX 服務(wù))。

下面給你 最簡潔、可直接運行 的完整步驟,復(fù)制粘貼就能用。

一、核心依賴(只需這一個)

pom.xml 中添加 Spring 官方 WebService 啟動器:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<!-- 可選:XML 解析工具,處理 SOAP 響應(yīng) -->
<dependency>
    <groupId>org.dom4j</groupId>
    <artifactId>dom4j</artifactId>
    <version>2.1.4</version>
</dependency>

二、配置 WebServiceTemplate

創(chuàng)建配置類,初始化模板并指定 WebService 地址:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.ws.client.core.WebServiceTemplate;
@Configuration
public class WebServiceConfig {
    /**
     * 配置 WebService 客戶端模板
     */
    @Bean
    public WebServiceTemplate webServiceTemplate() {
        WebServiceTemplate template = new WebServiceTemplate();
        // 設(shè)置你的 WebService 地址(.asmx 地址)
        template.setDefaultUri("http://10.38.113.72/WebService1.asmx");
        // 編碼格式
        template.setMessageSender(messageSender());
        return template;
    }
    /**
     * HTTP 消息發(fā)送器(設(shè)置超時、編碼)
     */
    @Bean
    public org.springframework.ws.transport.http.HttpComponentsMessageSender messageSender() {
        org.springframework.ws.transport.http.HttpComponentsMessageSender sender = new org.springframework.ws.transport.http.HttpComponentsMessageSender();
        sender.setConnectionTimeout(5000);
        sender.setReadTimeout(10000);
        return sender;
    }
}

三、編寫調(diào)用工具類(兩種方式任選)

方式 1:直接發(fā)送 SOAP 字符串(最簡單,無需生成代碼)

適合快速調(diào)用,手動拼 XML 報文,最通用。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.soap.client.core.SoapActionCallback;
@Service
public class SoapClientService {
    @Autowired
    private WebServiceTemplate webServiceTemplate;
    /**
     * 調(diào)用 WebService 方法
     * @param param 傳入?yún)?shù)
     * @return 返回結(jié)果
     */
    public String callWebService(String param) {
        // ===================== 1. 拼接 SOAP 請求 XML =====================
        // 從瀏覽器打開 ?wsdl 查看你的方法名和命名空間
        String soapXml = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
                "  <soap:Body>\n" +
                "    <HelloWorld xmlns=\"http://tempuri.org/\">\n" +
                "      <name>" + param + "</name>\n" +
                "    </HelloWorld>\n" +
                "  </soap:Body>\n" +
                "</soap:Envelope>";
        // ===================== 2. 設(shè)置 SOAPAction(.NET 必須) =====================
        // 格式:命名空間 + 方法名
        String soapAction = "http://tempuri.org/HelloWorld";
        SoapActionCallback callback = new SoapActionCallback(soapAction);
        // ===================== 3. 發(fā)送請求并獲取響應(yīng) =====================
        Object response = webServiceTemplate.marshalSendAndReceive(soapXml, callback);
        // ===================== 4. 把響應(yīng)轉(zhuǎn)成字符串 =====================
        return response.toString();
    }
}

方式 2:使用 JAXB 對象自動轉(zhuǎn)換(更優(yōu)雅)

推薦正式項目使用,自動把 Java 對象轉(zhuǎn)成 SOAP 請求,自動解析響應(yīng)。

1)根據(jù) WSDL 生成實體類(JDK 自帶工具)

打開命令行執(zhí)行:

wsimport -p com.example.dto -keep http://10.38.113.72/WebService1.asmx?wsdl

會自動生成請求、響應(yīng)、接口類。

2)配置轉(zhuǎn)換器

@Bean
public Jaxb2Marshaller marshaller() {
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    // 填寫生成類所在的包
    marshaller.setContextPath("com.example.dto");
    return marshaller;
}
@Bean
public WebServiceTemplate webServiceTemplate(Jaxb2Marshaller marshaller) {
    WebServiceTemplate template = new WebServiceTemplate();
    template.setDefaultUri("http://10.38.113.72/WebService1.asmx");
    // 綁定序列化工具
    template.setMarshaller(marshaller);
    template.setUnmarshaller(marshaller);
    return template;
}

3)直接用對象調(diào)用

public String callWithObject(String name) {
    // 生成的請求對象
    HelloWorld request = new HelloWorld();
    request.setName(name);
    // 調(diào)用
    HelloWorldResponse response = (HelloWorldResponse) webServiceTemplate.marshalSendAndReceive(request);
    return response.getHelloWorldResult();
}

四、關(guān)鍵注意事項(調(diào)用 .NET 必看)

  • 必須加 SOAPAction.NET 的 WebService 強制要求請求頭帶 SOAPAction,格式:
http://tempuri.org/方法名
  • XML 命名空間必須正確通常是:xmlns="http://tempuri.org/"
  • 請求 XML 格式直接瀏覽器打開你的服務(wù):http://10.38.113.72/WebService1.asmx點擊方法 → 就能看到官方示例 SOAP 請求體,直接復(fù)制使用即可。

五、測試調(diào)用

@Autowired
private SoapClientService soapClientService;
@Test
void testSoap() {
    String result = soapClientService.callWebService("張三");
    System.out.println("返回結(jié)果:" + result);
}

總結(jié)

  1. 依賴:只需要 spring-boot-starter-web-services
  2. 核心工具WebServiceTemplate
  3. 調(diào)用方式
    • 簡單場景:直接拼 XML 字符串
    • 正式項目:wsimport 生成對象 + JAXB 自動轉(zhuǎn)換
  4. .NET 必備SOAPAction 請求頭

如果你愿意,我可以根據(jù)你實際的 WSDL 地址,直接給你生成可運行的完整調(diào)用代碼,你只需要復(fù)制到項目里就能用!

到此這篇關(guān)于Spring Boot 使用 WebServiceTemplate 調(diào)用 WebService的完整步驟的文章就介紹到這了,更多相關(guān)Spring Boot調(diào)用 WebService內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot控制臺秒變炫彩特效的實現(xiàn)指南

    SpringBoot控制臺秒變炫彩特效的實現(xiàn)指南

    本文介紹如何自定義SpringBoot啟動橫幅,通過banner.txt文件設(shè)置ASCII圖案、版本號等,并使用ANSI顏色增強效果,還提供關(guān)閉默認Banner、添加啟動信息及彩色提示的實現(xiàn)方法,需要的朋友可以參考下
    2025-10-10
  • Java try catch finally的執(zhí)行順序解讀

    Java try catch finally的執(zhí)行順序解讀

    這篇文章主要介紹了Java try catch finally的執(zhí)行順序,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-05-05
  • SpringBoot實現(xiàn)異步的八種方法

    SpringBoot實現(xiàn)異步的八種方法

    Spring Boot 的異步處理主要是通過非阻塞I/O和回調(diào)機制來實現(xiàn)的,目的是提高應(yīng)用的并發(fā)性能,它支持多種方式來創(chuàng)建異步任務(wù),本文給大家介紹了SpringBoot實現(xiàn)異步的八種方法,需要的朋友可以參考下
    2024-07-07
  • Git工具 conflict沖突問題解決方案

    Git工具 conflict沖突問題解決方案

    這篇文章主要介紹了Git工具 conflict沖突問題解決方案,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-09-09
  • JAVA--HashMap熱門面試題

    JAVA--HashMap熱門面試題

    這篇文章主要介紹了JAVA關(guān)于HashMap容易被提問的面試題,文中題目提問頻率高,相信對你的面試有一定幫助,想要入職JAVA的朋友可以了解下
    2020-06-06
  • Java實現(xiàn)讀取Jar文件屬性的方法詳解

    Java實現(xiàn)讀取Jar文件屬性的方法詳解

    這篇文章主要為大家詳細介紹了如何利用Java語言實現(xiàn)讀取Jar文件屬性的功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2022-08-08
  • 詳解Java刪除Map中元素java.util.ConcurrentModificationException”異常解決

    詳解Java刪除Map中元素java.util.ConcurrentModificationException”異常解決

    這篇文章主要介紹了詳解Java刪除Map中元素java.util.ConcurrentModificationException”異常解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • SpringCloud微服務(wù)踩坑記錄分享

    SpringCloud微服務(wù)踩坑記錄分享

    本文記錄了作者在使用SpringCloud微服務(wù)時遇到的問題,首先,作者嘗試修改配置文件中的service-name和instance-id,但仍然無法解決問題,后來,作者嘗試更換SpringCloud版本為2.2.5,并搭配Hoxton.SR3版本,問題得以解決
    2024-11-11
  • Redis內(nèi)存數(shù)據(jù)庫示例分析

    Redis內(nèi)存數(shù)據(jù)庫示例分析

    Redis本身的內(nèi)容比較復(fù)雜。如果你上來,你應(yīng)該研究一個細節(jié)點,比如連接池和數(shù)據(jù)結(jié)構(gòu)。雖然可以直接了解某一點的詳細來源內(nèi)容,甚至盡快解決一些意外,但是容易淹沒在失眠的細節(jié)中,整體控制不了Redis
    2022-12-12
  • 深入剖析java中的集合框架

    深入剖析java中的集合框架

    下面小編就為大家?guī)硪黄钊肫饰鰆ava中的集合框架。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-05-05

最新評論

民丰县| 芜湖县| 南丰县| 文安县| 延安市| 星子县| 太保市| 新巴尔虎右旗| 新安县| 罗定市| 荃湾区| 阜平县| 绥滨县| 嘉义县| 安塞县| 中西区| 宿州市| 鹰潭市| 天门市| 都安| 娄底市| 武宁县| 三河市| 巧家县| 天镇县| 肃北| 尚义县| 双柏县| 通化市| 庄浪县| 社会| 淅川县| 体育| 中卫市| 个旧市| 黄骅市| 井研县| 郧西县| 普兰店市| 家居| 上杭县|