<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Spring Boot SOAP Web 服務示例 > 原文: [https://howtodoinjava.com/spring-boot/spring-boot-soap-webservice-example/](https://howtodoinjava.com/spring-boot/spring-boot-soap-webservice-example/) 了解如何利用 Spring Boot 的簡便性來快速**創建 SOAP Web 服務**。 [REST](https://restfulapi.net) 和[微服務](https://howtodoinjava.com/microservices/microservices-definition-principles-benefits/)每天都在流行,但是 [SOAP](https://en.wikipedia.org/wiki/SOAP) 在某些情況下仍然有自己的地位。 在本 **SpringBoot SOAP 教程**中,我們將僅關注與 SpringBoot 相關的配置,以了解我們可以多么輕松地創建我們的**第一個 SOAP Webservice** 。 我們將構建一個簡單的合同優先的 SOAP Web 服務,在其中我們將使用硬編碼后端實現學生搜索功能,以進行演示。 ## 1\. 技術棧 * JDK 1.8,Eclipse,Maven – 開發環境 * SpringBoot – 基礎應用程序框架 * [wsdl4j](https://mvnrepository.com/artifact/wsdl4j/wsdl4j) – 為我們的服務發布 WSDL * [SOAP-UI](https://www.soapui.org/) – 用于測試我們的服務 * [JAXB maven 插件](https://www.mojohaus.org/jaxb2-maven-plugin/Documentation/v2.2/) – 用于代碼生成 ## 2\. 項目結構 為該演示創建的類和文件如下所示。 ![Spring Boot SOAP WS Project Structure](https://img.kancloud.cn/20/2b/202bbcc6e111976bbf95289ed57ce904_414x592.jpg) Spring Boot SOAP WS 項目結構 ## 3\. 創建 Spring Boot 項目 僅從具有`Web Services`依賴關系的[ SPRING 初始化器](https://start.spring.io/)站點創建一個 spring boot 項目。 選擇依賴項并提供適當的 Maven GAV 坐標后,以壓縮格式下載項目。 解壓縮,然后將 eclipse 中的項目導入為 maven 項目。 ![](https://img.kancloud.cn/52/da/52dabe1af9561bba314d678beab6baf1_1360x701.jpg) Spring boot 項目生成 #### 添加 Wsdl4j 依賴關系 編輯`pom.xml`并將此依賴項添加到您的項目中。 ```java <dependency> <groupId>wsdl4j</groupId> <artifactId>wsdl4j</artifactId> </dependency> ``` ## 4\. 創建 SOAP 域模型并生成 Java 代碼 當我們遵循合同優先的方法來開發服務時,我們需要首先為我們的服務創建域(方法和參數)。 為簡單起見,我們將請求和響應都保留在相同的 [XSD](https://www.w3.org/TR/xmlschema11-1/) 中,但在實際的企業用例中,我們將有多個 XSD 相互導入以形成最終定義。 `student.xsd` ```java <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="https://www.howtodoinjava.com/xml/school" targetNamespace="https://www.howtodoinjava.com/xml/school" elementFormDefault="qualified"> <xs:element name="StudentDetailsRequest"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="StudentDetailsResponse"> <xs:complexType> <xs:sequence> <xs:element name="Student" type="tns:Student"/> </xs:sequence> </xs:complexType> </xs:element> <xs:complexType name="Student"> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="standard" type="xs:int"/> <xs:element name="address" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:schema> ``` 將以上文件放置在項目的`resources`文件夾中。 #### 將 XSD 的 JAXB maven 插件添加到 Java 對象生成 我們將使用`jaxb2-maven-plugin`有效地生成域類。 現在,我們需要將以下 Maven 插件添加到項目的`pom.xml`文件的插件部分。 ```java <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</artifactId> <version>1.6</version> <executions> <execution> <id>xjc</id> <goals> <goal>xjc</goal> </goals> </execution> </executions> <configuration> <schemaDirectory>${project.basedir}/src/main/resources/</schemaDirectory> <outputDirectory>${project.basedir}/src/main/java</outputDirectory> <clearOutputDir>false</clearOutputDir> </configuration> </plugin> ``` 該插件使用 [XJC](https://docs.oracle.com/javase/8/docs/technotes/tools/unix/xjc.html) 工具作為代碼生成引擎。 XJC 將 XML 模式文件編譯為完全注解的 Java 類。 現在執行上面的 maven 插件以從 XSD 生成 Java 代碼。 ## 5\. 創建 SOAP Web 服務端點 `StudentEndpoint`類將處理對服務的所有傳入請求,并將調用委派給數據存儲庫的`finder`方法。 ```java package com.example.howtodoinjava.springbootsoapservice; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ws.server.endpoint.annotation.Endpoint; import org.springframework.ws.server.endpoint.annotation.PayloadRoot; import org.springframework.ws.server.endpoint.annotation.RequestPayload; import org.springframework.ws.server.endpoint.annotation.ResponsePayload; import com.howtodoinjava.xml.school.StudentDetailsRequest; import com.howtodoinjava.xml.school.StudentDetailsResponse; @Endpoint public class StudentEndpoint { private static final String NAMESPACE_URI = "https://www.howtodoinjava.com/xml/school"; private StudentRepository StudentRepository; @Autowired public StudentEndpoint(StudentRepository StudentRepository) { this.StudentRepository = StudentRepository; } @PayloadRoot(namespace = NAMESPACE_URI, localPart = "StudentDetailsRequest") @ResponsePayload public StudentDetailsResponse getStudent(@RequestPayload StudentDetailsRequest request) { StudentDetailsResponse response = new StudentDetailsResponse(); response.setStudent(StudentRepository.findStudent(request.getName())); return response; } } ``` 這里有一些關于注解的細節: 1. [`@Endpoint`](https://docs.spring.io/spring-ws/site/apidocs/org/springframework/ws/server/endpoint/annotation/Endpoint.html)向 Spring WS 注冊該類,作為處理傳入 SOAP 消息的潛在候選者。 2. 然后,Spring WS 使用[`@PayloadRoot`](https://docs.spring.io/spring-ws/site/apidocs/org/springframework/ws/server/endpoint/annotation/PayloadRoot.html)根據消息的名稱空間和 localPart 選擇處理器方法。 請注意此注解中提到的命名空間 URL 和請求載荷根請求。 3. [`@RequestPayload`](https://docs.spring.io/spring-ws/site/apidocs/org/springframework/ws/server/endpoint/annotation/RequestPayload.html)表示傳入的消息將被映射到方法的請求參數。 4. [`@ResponsePayload`](https://docs.spring.io/spring-ws/site/apidocs/org/springframework/ws/server/endpoint/annotation/ResponsePayload.html)注解使 Spring WS 將返回的值映射到響應載荷。 #### 創建數據存儲庫 如前所述,我們將使用硬編碼的數據作為此演示的后端,讓我們添加一個名為`StudentRepository.java`并帶有 Spring `@Repository`注解的類。 它只會將數據保存在`HashMap`中,并且還會提供一種稱為`findStudent()`的查找器方法。 > 閱讀更多 – [`@Repository`注解](https://howtodoinjava.com/spring/spring-core/how-to-use-spring-component-repository-service-and-controller-annotations/) ```java package com.example.howtodoinjava.springbootsoapservice; import java.util.HashMap; import java.util.Map; import javax.annotation.PostConstruct; import org.springframework.stereotype.Component; import org.springframework.util.Assert; import com.howtodoinjava.xml.school.Student; @Component public class StudentRepository { private static final Map<String, Student> students = new HashMap<>(); @PostConstruct public void initData() { Student student = new Student(); student.setName("Sajal"); student.setStandard(5); student.setAddress("Pune"); students.put(student.getName(), student); student = new Student(); student.setName("Kajal"); student.setStandard(5); student.setAddress("Chicago"); students.put(student.getName(), student); student = new Student(); student.setName("Lokesh"); student.setStandard(6); student.setAddress("Delhi"); students.put(student.getName(), student); student = new Student(); student.setName("Sukesh"); student.setStandard(7); student.setAddress("Noida"); students.put(student.getName(), student); } public Student findStudent(String name) { Assert.notNull(name, "The Student's name must not be null"); return students.get(name); } } ``` ## 6\. 添加 SOAP Web 服務配置 Bean 創建帶有`@Configuration`注解的類以保存 bean 定義。 ```java package com.example.howtodoinjava.springbootsoapservice; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.ws.config.annotation.EnableWs; import org.springframework.ws.config.annotation.WsConfigurerAdapter; import org.springframework.ws.transport.http.MessageDispatcherServlet; import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition; import org.springframework.xml.xsd.SimpleXsdSchema; import org.springframework.xml.xsd.XsdSchema; @EnableWs @Configuration public class Config extends WsConfigurerAdapter { @Bean public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) { MessageDispatcherServlet servlet = new MessageDispatcherServlet(); servlet.setApplicationContext(applicationContext); servlet.setTransformWsdlLocations(true); return new ServletRegistrationBean(servlet, "/service/*"); } @Bean(name = "studentDetailsWsdl") public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema countriesSchema) { DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition(); wsdl11Definition.setPortTypeName("StudentDetailsPort"); wsdl11Definition.setLocationUri("/service/student-details"); wsdl11Definition.setTargetNamespace("https://www.howtodoinjava.com/xml/school"); wsdl11Definition.setSchema(countriesSchema); return wsdl11Definition; } @Bean public XsdSchema countriesSchema() { return new SimpleXsdSchema(new ClassPathResource("school.xsd")); } } ``` * `Config`類擴展了[`WsConfigurerAdapter`](https://docs.spring.io/spring-ws/docs/current/api/org/springframework/ws/config/annotation/WsConfigurerAdapter.html),它配置了注解驅動的 Spring-WS 編程模型。 * [`MessageDispatcherServlet`](https://docs.spring.io/spring-ws/site/apidocs/org/springframework/ws/transport/http/MessageDispatcherServlet.html) – Spring-WS 使用它來處理 SOAP 請求。 我們需要向該 servlet 注入`ApplicationContext`,以便 Spring-WS 找到其他 bean。 它還聲明了請求的 URL 映射。 * `DefaultWsdl11Definition`使用`XsdSchema`公開了標準的 WSDL 1.1。 Bean 名稱`studentDetailsWsdl`將是將公開的 wsdl 名稱。 它可以在 http:// localhost:8080 / service / studentDetailsWsdl.wsdl 下找到。 這是在 Spring 公開合約優先的 wsdl 的最簡單方法。 此配置還在內部使用 WSDL 位置 servlet 轉換`servlet.setTransformWsdlLocations( true )`。 如果我們看到導出的 WSDL,則`soap:address`將具有`localhost`地址。 同樣,如果我們改為從分配給已部署機器的面向公眾的 IP 地址訪問 WSDL,我們將看到該地址而不是`localhost`。 因此,端點 URL 根據部署環境是動態的。 ## 7\. Spring Boot SOAP Web 服務演示 使用`mvn clean install`進行 maven 構建,然后使用`java -jar target\spring-boot-soap-service-0.0.1-SNAPSHOT.jar`命令啟動應用程序。 這將在默認端口`8080`中啟動一臺 tomcat 服務器,并將在其中部署應用程序。 1)現在轉到`http://localhost:8080/service/studentDetailsWsdl.wsdl`,查看 WSDL 是否正常運行。 ![WSDL generated](https://img.kancloud.cn/7f/5e/7f5eaf80e68ae2319bc7eb0d2daa0cd4_1360x649.jpg) WSDL 已生成 2)一旦成功生成了 WSDL,就可以使用該 WSDL 在 SOAP ui 中創建一個項目并測試該應用程序。 樣品請求和響應如下。 **請求:** ```java <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sch="https://www.howtodoinjava.com/xml/school"> <soapenv:Header/> <soapenv:Body> <sch:StudentDetailsRequest> <sch:name>Sajal</sch:name> </sch:StudentDetailsRequest> </soapenv:Body> </soapenv:Envelope> ``` **響應:** ```java <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> ``` ![SOAP UI Example](https://img.kancloud.cn/a8/bf/a8bfcaf5ac02ea55ca9df5f826a5f1d9_1365x723.jpg) SOAP UI 示例 ## 8\. 總結 在上面的示例中,我們學習了**使用 Spring Boot** 創建 SOAP Web 服務。 我們還學習了**從 WSDL** 生成 Java 代碼。 我們了解了處理 SOAP 請求所需的 bean。 如果您在執行上述項目時遇到任何困難,請隨時發表評論。 [本文的源碼](https://howtodoinjava.com/wp-content/uploads/2017/10/spring-boot-soap-service.zip) 學習愉快! 閱讀更多: [Spring Boot Soap Web 服務客戶端示例](https://howtodoinjava.com/spring-boot/spring-soap-client-webservicetemplate/)
                  <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>

                              哎呀哎呀视频在线观看