欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot調(diào)用WebService接口的實(shí)現(xiàn)示例

 更新時(shí)間:2025年03月12日 08:27:18   作者:m0_74823827  
本文主要介紹了SpringBoot調(diào)用WebService接口的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言

調(diào)用WebService接口的方式有很多,今天記錄一下,使用 Spring Web Services 調(diào)用 SOAP WebService接口

一.導(dǎo)入依賴

        <!-- Spring Boot Web依賴 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Spring Web Services -->
        <dependency>
            <groupId>org.springframework.ws</groupId>
            <artifactId>spring-ws-core</artifactId>
        </dependency>

        <!-- Apache HttpClient 作為 WebService 客戶端 -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>

        <!-- JAXB API -->
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.1</version>
        </dependency>

        <!-- JAXB 運(yùn)行時(shí) -->
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.3.5</version>
        </dependency>

二.創(chuàng)建請(qǐng)求類和響應(yīng)類

根據(jù)SOAP的示例,創(chuàng)建請(qǐng)求類和響應(yīng)類

SOAP示例

請(qǐng)求
POST *****************
Host: **************
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://*******/DownloadFileByMaterialCode"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <DownloadFileByMaterialCode xmlns="http://*******/">
      <MaterialCode>string</MaterialCode>
      <FileType>string</FileType>
      <Category>string</Category>
    </DownloadFileByMaterialCode>
  </soap:Body>
</soap:Envelope>


響應(yīng)
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <DownloadFileByMaterialCodeResponse xmlns="http://********/">
      <DownloadFileByMaterialCodeResult>string</DownloadFileByMaterialCodeResult>
    </DownloadFileByMaterialCodeResponse>
  </soap:Body>
</soap:Envelope>

根據(jù)我的這個(gè)示例,我創(chuàng)建的請(qǐng)求類和響應(yīng)類,是這樣的

請(qǐng)求類

@Data
@XmlRootElement(name = "DownloadFileByMaterialCode", namespace = "http://*******/")
@XmlAccessorType(XmlAccessType.FIELD)
public class DownloadFileByMaterialCodeRequest {
    @XmlElement(name = "MaterialCode", namespace = "http://*******/")
    private String MaterialCode;
    @XmlElement(name = "FileType", namespace = "http://*******/")
    private String FileType;
    @XmlElement(name = "Category", namespace = "http://*******/")
    private String Category;
}

響應(yīng)類

@Data
@XmlRootElement(name = "DownloadFileByMaterialCodeResponse", namespace = "http://********/")
@XmlAccessorType(XmlAccessType.FIELD)
public class DownloadFileByMaterialCodeResponse {
    @XmlElement(name = "DownloadFileByMaterialCodeResult", namespace = "http://********/")
    private String DownloadFileByMaterialCodeResult;
}

三.創(chuàng)建ObjectFactory類

@XmlRegistry
public class ObjectFactory {
    // 創(chuàng)建 DownloadFileByMaterialCodeRequest 的實(shí)例
    public DownloadFileByMaterialCodeRequest createDownloadFileByMaterialCodeRequest() {
        return new DownloadFileByMaterialCodeRequest();
    }

    // 創(chuàng)建 DownloadFileByMaterialCodeResponse 的實(shí)例
    public DownloadFileByMaterialCodeResponse createDownloadFileByMaterialCodeResponse() {
        return new DownloadFileByMaterialCodeResponse();
    }
}

四.配置WebServiceTemplate

@Configuration
public class WebServiceConfig {

    @Bean
    public WebServiceTemplate webServiceTemplate() {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setContextPath("org.jeecg.modules.webservice");  // 包路徑,包含請(qǐng)求和響應(yīng)對(duì)象類

        WebServiceTemplate template = new WebServiceTemplate(marshaller);

        return template;
    }
}

五.調(diào)用WebService接口

@Service
public class DownloadFileService {
    @Autowired
    private WebServiceTemplate webServiceTemplate;

    public List<ResponseJsonObject> downloadFile(String materialCode, String fileType, String category) throws JsonProcessingException {
        String uri = "http://192.168.***.***/DYDServiceTest/PlmService.asmx";  // WebService 的 URL

        // 創(chuàng)建請(qǐng)求對(duì)象并設(shè)置參數(shù)
        DownloadFileByMaterialCodeRequest request = new DownloadFileByMaterialCodeRequest();
        request.setMaterialCode(materialCode);
        request.setFileType(fileType);
        request.setCategory(category);

        // 設(shè)置 SOAPAction
        String soapAction = "http://********/DownloadFileByMaterialCode";  // Web 服務(wù)指定的 SOAPAction

        // 使用 SoapActionCallback 來設(shè)置 SOAPAction 頭
        SoapActionCallback soapActionCallback = new SoapActionCallback(soapAction);

        // 發(fā)送 SOAP 請(qǐng)求并獲取響應(yīng)
        DownloadFileByMaterialCodeResponse response = (DownloadFileByMaterialCodeResponse)
                webServiceTemplate.marshalSendAndReceive(uri, request, soapActionCallback);

        // 獲取并返回 DownloadFileByMaterialCodeResult
        String downloadFileByMaterialCodeResult = response.getDownloadFileByMaterialCodeResult();
        System.out.println(downloadFileByMaterialCodeResult);

        //字符串轉(zhuǎn)換為ResponseJsonObject對(duì)象
        ObjectMapper objectMapper = new ObjectMapper();
        // 忽略未知字段
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        List<ResponseJsonObject> ResponseJsonObjects = objectMapper.readValue(downloadFileByMaterialCodeResult, objectMapper.getTypeFactory().constructCollectionType(List.class, ResponseJsonObject.class));

        return ResponseJsonObjects;
    }
}

六.測(cè)試代碼

    @Test
    public void test1() throws JsonProcessingException {
        List<ResponseJsonObject> responseJsonObjects = downloadFileService.downloadFile("CCPT0016-QBY-7", "", "");
        for (ResponseJsonObject responseJsonObject : responseJsonObjects) {
            System.out.println(responseJsonObject.getDocName());
        }
    }

測(cè)試效果

這里在附上所有文件的路勁圖,可以參考一下

總結(jié)

根據(jù)接口給出的SAOP的示例,封裝好對(duì)應(yīng)的實(shí)體類,因?yàn)槲疫@里的類型都是String,大家也可以根據(jù)實(shí)際情況,封裝好對(duì)應(yīng)的類

注意注解的參數(shù),namespace = “http://*******/” 給接口提供的域名地址

到此這篇關(guān)于SpringBoot調(diào)用WebService接口的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)SpringBoot調(diào)用WebService接口內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 通過JDBC連接oracle數(shù)據(jù)庫的十大技巧

    通過JDBC連接oracle數(shù)據(jù)庫的十大技巧

    通過JDBC連接oracle數(shù)據(jù)庫的十大技巧...
    2006-12-12
  • Java并發(fā)編程示例(七):守護(hù)線程的創(chuàng)建和運(yùn)行

    Java并發(fā)編程示例(七):守護(hù)線程的創(chuàng)建和運(yùn)行

    這篇文章主要介紹了Java并發(fā)編程示例(七):守護(hù)線程的創(chuàng)建和運(yùn)行,在本節(jié)示例中,我們將創(chuàng)建兩個(gè)線程,一個(gè)是普通線程,向隊(duì)列中寫入事件,另外一個(gè)是守護(hù)線程,清除隊(duì)列中的事件,需要的朋友可以參考下
    2014-12-12
  • 淺析Spring獲取Bean的九種方法詳解

    淺析Spring獲取Bean的九種方法詳解

    隨著SpringBoot的普及,Spring的使用也越來越廣,在某些場(chǎng)景下,我們無法通過注解或配置的形式直接獲取到某個(gè)Bean。比如,在某一些工具類、設(shè)計(jì)模式實(shí)現(xiàn)中需要使用到Spring容器管理的Bean,此時(shí)就需要直接獲取到對(duì)應(yīng)的Bean,這篇文章主要介紹了Spring獲取Bean的九種方法
    2023-01-01
  • 帶你快速了解Java中類和對(duì)象的關(guān)系

    帶你快速了解Java中類和對(duì)象的關(guān)系

    這篇文章主要給大家介紹了關(guān)于Java中類和對(duì)象關(guān)系的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • Spring 依賴注入的幾種方式詳解

    Spring 依賴注入的幾種方式詳解

    本篇文章主要介紹了Spring 依賴注入的幾種方式詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-02-02
  • Java中ArrayList和LinkedList區(qū)別

    Java中ArrayList和LinkedList區(qū)別

    這篇文章主要介紹了Java中ArrayList和LinkedList區(qū)別,下面我們就重點(diǎn)聊一聊在日常開發(fā)中經(jīng)常被使用到的兩個(gè)集合類ArrayList和LinkedList的本質(zhì)區(qū)別吧,需要的朋友可以參考一下
    2022-01-01
  • Java?HttpURLConnection使用方法與實(shí)例演示分析

    Java?HttpURLConnection使用方法與實(shí)例演示分析

    這篇文章主要介紹了Java?HttpURLConnection使用方法與實(shí)例演示,HttpURLConnection一個(gè)抽象類是標(biāo)準(zhǔn)的JAVA接口,該類位于java.net包中,它提供了基本的URL請(qǐng)求,響應(yīng)等功能,下面我們來深入看看
    2023-10-10
  • 使用JWT創(chuàng)建解析令牌及RSA非對(duì)稱加密詳解

    使用JWT創(chuàng)建解析令牌及RSA非對(duì)稱加密詳解

    這篇文章主要介紹了JWT創(chuàng)建解析令牌及RSA非對(duì)稱加密詳解,JWT是JSON Web Token的縮寫,即JSON Web令牌,是一種自包含令牌,一種情況是webapi,類似之前的阿里云播放憑證的功能,另一種情況是多web服務(wù)器下實(shí)現(xiàn)無狀態(tài)分布式身份驗(yàn)證,需要的朋友可以參考下
    2023-11-11
  • Spring?Security+JWT如何實(shí)現(xiàn)前后端分離權(quán)限控制

    Spring?Security+JWT如何實(shí)現(xiàn)前后端分離權(quán)限控制

    本篇將手把手教你用?Spring?Security?+?JWT?搭建一套完整的登錄認(rèn)證與權(quán)限控制體系,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • javaSE基礎(chǔ)如何通俗的理解javaBean是什么

    javaSE基礎(chǔ)如何通俗的理解javaBean是什么

    所謂的Java Bean,就是一個(gè)java類,編譯后成為了一個(gè)后綴名是 .class的文件。這就是Java Bean,很多初學(xué)者,包括當(dāng)年的我自己,總是被這些專有名詞搞的暈頭轉(zhuǎn)向
    2021-10-10

最新評(píng)論