<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Spring Boot SOAP 客戶端 – `WebServiceTemplate`示例 > 原文: [https://howtodoinjava.com/spring-boot/spring-soap-client-webservicetemplate/](https://howtodoinjava.com/spring-boot/spring-soap-client-webservicetemplate/) 學習使用 Spring Boot Soap 客戶端使用 SOAP Web 服務以及使用 JAXB maven 插件自動生成客戶端代理類。 創建 SOAP Web 服務不在本教程的討論范圍內,但是您可以在中學習它。 > 閱讀更多:[在 Spring Framework 中創建 SOAP WS](https://howtodoinjava.com/spring/spring-boot/spring-boot-soap-webservice-example/)。 ## 先決條件 在運行此示例之前,我們需要準備好一個 SOAP 服務,該服務將從該客戶端代碼中調用。 為此,您可以下載附件的 maven 項目(在文章結尾),然后在本地工作區中運行它并使用它。 運行此 SOAP 服務器項目后,您將從`http://localhost:8080/service/studentDetailsWsdl.wsdl`獲取 WSDL。 將 WSDL 下載為`studentDetailsWsdl.wsdl`,稍后將其放置在客戶端項目的`resources/wsdl`文件夾中,該文件夾將在下一步創建以生成客戶端代理代碼。 ## Spring Boot Soap 客戶端的技術棧 * JDK 1.8,Eclipse,Maven – 開發環境 * SpringBoot – 基礎應用程序框架 * `maven-jaxb2-plugin`插件 – 用于生成 JAXB 存根 * SpringBoot `CommandLineRunner` – 測試客戶端代碼 ## 項目結構 為該演示創建的類和文件如下所示。 ![SOAP client project structure](https://img.kancloud.cn/7f/4c/7f4c2b2f92867d5f3aea6e0c30b7bac8_435x603.jpg) SOAP 客戶端項目結構 ## 使用`WebServiceTemplate`創建 Spring 客戶端 #### 創建啟動項目 僅從具有`Web Services`依賴關系的[ SPRING 初始化器](https://start.spring.io/)站點創建一個 spring boot 項目。 選擇依賴項并提供適當的 Maven GAV 坐標后,以壓縮格式下載項目。 解壓縮,然后將 eclipse 中的項目導入為 maven 項目。 ![Generate Spring boot project](https://img.kancloud.cn/5c/8d/5c8d41884b4df49330b9f5142d2e0381_1362x702.jpg) Spring boot 項目生成 #### 生成 SOAP 域類 現在使用`maven-jaxb2-plugin` maven 插件生成 **JAXB 注解的存根類**。 為此,將此 maven 插件添加到項目的`pom.xml`中。 `pom.xml` ```java <plugin> <groupId>org.jvnet.jaxb2.maven2</groupId> <artifactId>maven-jaxb2-plugin</artifactId> <version>0.13.2</version> <executions> <execution> <goals> <goal>generate</goal> </goals> </execution> </executions> <configuration> <generatePackage>com.example.howtodoinjava.schemas.school</generatePackage> <generateDirectory>${project.basedir}/src/main/java</generateDirectory> <schemaDirectory>${project.basedir}/src/main/resources/wsdl</schemaDirectory> <schemaIncludes> <include>*.wsdl</include> </schemaIncludes> </configuration> </plugin> ``` 此插件將在項目的`src`目錄的`com.example.howtodoinjava.springbootsoapclient`包中生成類,并且此插件將檢查類的生成時間戳,以便僅在`WSDL`中發生任何更改時才生成這些類。 #### 使用`WebServiceTemplate`創建 SOAP 客戶端 創建一個名為`SOAPConnector.java`的類,該類將充當對 Web 服務的所有請求的通用 Web 服務客戶端。 `SOAPConnector.java` ```java package com.example.howtodoinjava.springbootsoapclient; import org.springframework.ws.client.core.support.WebServiceGatewaySupport; public class SOAPConnector extends WebServiceGatewaySupport { public Object callWebService(String url, Object request){ return getWebServiceTemplate().marshalSendAndReceive(url, request); } } ``` 1. `SOAPConnector`類是對`WebServiceGatewaySupport`的擴展,它基本上是通過`getWebServiceTemplate()`方法提供的`WebServiceTemplate`內部實現注入一個接口。 2. 我們將使用此`WebServiceTemplate`來調用 SOAP 服務。 3. 該類還期望注入一個名為`Marshaller`和`Unmarshaller`的 spring bean,它們將由配置類提供,我們將在下面看到。 #### Spring bean 配置 現在,我們需要創建一個用`@Configuration`注解的配置類,該類將具有`SOAPConnector`所需的必需的 bean 定義,以使其正常工作。 `Config.java` ```java package com.example.howtodoinjava.springbootsoapclient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.oxm.jaxb.Jaxb2Marshaller; @Configuration public class Config { @Bean public Jaxb2Marshaller marshaller() { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); // this is the package name specified in the <generatePackage> specified in // pom.xml marshaller.setContextPath("com.example.howtodoinjava.schemas.school"); return marshaller; } @Bean public SOAPConnector soapConnector(Jaxb2Marshaller marshaller) { SOAPConnector client = new SOAPConnector(); client.setDefaultUri("http://localhost:8080/service/student-details"); client.setMarshaller(marshaller); client.setUnmarshaller(marshaller); return client; } } ``` 1. `WebServiceGatewaySupport`需要`Marshaller`和`Unmarshaller`,它們是`Jaxb2Marshaller`類的實例。 2. 它使用`com.example.howtodoinjava.schemas.school`作為 JAXB 類的基本包。 它將使用此包創建 JAXB 上下文。 3. 我們將使用此`Jaxb2Marshaller` bean 作為`SOAPConnector` bean 的`Marshaller/Unmarshaller`。 #### 使用`CommandLineRunner`測試 為簡單起見,我們將創建一個[ Spring Boot 命令行運行程序](https://howtodoinjava.com/spring/spring-boot/command-line-runner-interface-example/),該加載程序將加載 spring 上下文并調用處理器方法,并將命令行參數傳遞給該方法。 實時地,我們需要用一些其他代碼替換此命令行運行程序,這些代碼將更適合企業。 我們需要在`SpringBootApplication`類中添加此命令行運行器 bean,如下。 `SpringBootSoapClientApplication.java` ```java package com.example.howtodoinjava.springbootsoapclient; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import com.example.howtodoinjava.schemas.school.StudentDetailsRequest; import com.example.howtodoinjava.schemas.school.StudentDetailsResponse; @SpringBootApplication public class SpringBootSoapClientApplication { public static void main(String[] args) { SpringApplication.run(SpringBootSoapClientApplication.class, args); } @Bean CommandLineRunner lookup(SOAPConnector soapConnector) { return args -> { String name = "Sajal";//Default Name if(args.length>0){ name = args[0]; } StudentDetailsRequest request = new StudentDetailsRequest(); request.setName(name); StudentDetailsResponse response =(StudentDetailsResponse) soapConnector.callWebService("http://localhost:8080/service/student-details", request); System.out.println("Got Response As below ========= : "); System.out.println("Name : "+response.getStudent().getName()); System.out.println("Standard : "+response.getStudent().getStandard()); System.out.println("Address : "+response.getStudent().getAddress()); }; } } ``` 在這里,我們從命令行獲取搜索參數,并創建`StudentDetailsRequest`對象,并使用`SOAPConnector`調用 SOAP Web 服務。 #### 一些可選配置 打開`application.properties`并添加以下配置 `application.properties` ```java server.port = 9090 logging.level.org.springframework.ws=TRACE ``` 在這里,我們用`server.port = 9090`將默認端口覆蓋為`9090`,因為您已經注意到我們的示例 SOAP 服務在默認端口`8080`中運行,因為兩個 Java 進程不能在同一端口中運行。 另外,我們正在通過`logging.level.org.springframework.ws=TRACE`為`org.springframework.ws`軟件包啟用`TRACE`日志記錄。 這將在控制臺中打印 SOAP 負載。 這就是我們使用 Spring Boot 消費 SOAP 服務所需要做的一切,現在是時候進行測試了。 ## 示例 現在使用 maven 命令`mvn clean install`來構建應用程序。 我們可以從命令提示符下通過命令`java -jar target\spring-boot-soap-client-0.0.1-SNAPSHOT.jar Lokesh`調用命令行運行程序。 請注意,我們在此處傳遞了一個命令行參數`Lokesh`,該參數將在`CommandLineRunner` bean 的查找方法中使用。 如果沒有傳遞任何名稱,我們將在該方法中傳遞一個默認名稱。 調用命令行運行程序后,我們應該看到 SOAP 服務輸出,并且響應已正確解組到 JAXB 對象`StudentDetailsResponse`。 同樣,我們可以在 TRACE 日志中看到完整的 SOAP 請求/響應,如下所示。 #### 輸出 ```java 2017-10-09 23:20:45.548 TRACE 9204 --- [ main] o.s.ws.client.MessageTracing.received : Received response [<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><ns2:StudentDetailsResponse xmlns:ns2="https://www.howtodoinjava.com/xml/school"><ns2:Student><ns2:name>Sajal</ns2:name><ns2:standard>5</ns2:standard><ns2:address>Pune</ns2:address></ns2:Student></ns2:StudentDetailsResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>] for request [<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><ns2:StudentDetailsRequest xmlns:ns2="https://www.howtodoinjava.com/xml/school"><ns2:name>Sajal</ns2:name></ns2:StudentDetailsRequest></SOAP-ENV:Body></SOAP-ENV:Envelope>] Got Response As below ========= : Name : Lokesh Standard : 6 Address : Delhi ``` ## 總結 在本 SOAP 教程中,我們學習了如何輕松地從 Spring Boot Soap 客戶端中使用**來使用 SOAP 服務**。 每當需要使用任何此類 SOAP 服務時,都可以使用此方法。 希望這對您有用。 請在評論部分添加您的反饋。 [下載源碼](https://howtodoinjava.com/wp-content/uploads/2017/10/spring-boot-soap-client.zip) 學習愉快!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看