91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

spring boot 開發soap webservice的實現代碼

發布時間:2020-09-18 20:34:22 來源:腳本之家 閱讀:241 作者:wls1036 欄目:編程語言

介紹

spring boot web模塊提供了RestController實現restful,第一次看到這個名字的時候以為還有SoapController,很可惜沒有,對于soap webservice提供了另外一個模塊spring-boot-starter-web-services支持。本文介紹如何在spring boot中開發soap webservice接口,以及接口如何同時支持soap和restful兩種協議。

soap webservice

Web service是一個平臺獨立的,低耦合的,自包含的、基于可編程的web的應用程序,既可以是soap webservice也可以是rest webservice,在rest還沒出來之前,我們說webservice一般是指基于soap協議進行通信的web應用程序。

在開始之前,我覺得有必要了解下soap webservice,具體的概念網上可以找到很多資料,但網上資料概念性較強,而且soap協議使用的是xml進行通信,相信xml里面一個namespace就能嚇跑一大堆人,所以這里不討論具體的soap協議細節,我想通過一個例子來說明什么是soap webservice,通過該例子,你能了解soap webservice其運作原理,當然如果你覺得你對這個已經很了解了,大可跳過本章節,本章節跟后面的內容沒有任何關系。

假設我們開發了一個web接口,想給別人用,我們要怎么辦

  1. 部署接口到服務器
  2. 編寫接口文檔,寫清楚接口是通過什么方法調的,輸入參數是什么,輸出參數是什么,錯誤時返回什么。

那問題來了,我們能不能只把接口部署到服務器上,然后接口不單能提供具體的服務,而且還能自動生成一份標準的接口文檔,把接口信息都記錄在該文檔里,如果能做到,是不是能做到"接口即文檔"的目的。

那么一個接口的信息包括哪些呢?

  1. 接口地址
  2. 接口調用方法
  3. 接口輸入參數
  4. 接口輸出參數
  5. 接口出錯返回信息
  6. ....

soap webservice里wsdl文件就是接口描述信息。核心的信息就是以上幾個。

第二個問題,由于Web service是一個平臺獨立,也就是說,使用接口的人不知道這個service是用什么技術開發的,可能是php可能是java等,但接口的參數和返回的數據都是一樣的,要達到這種目的,就需要兩個東西,一個是跟平臺無關的數據格式,soap使用的是xml,一個是通信協議,也就是soap協議。

下面就介紹如何不使用任何框架,僅通過servlet實現一個webservice。該webservice功能很簡單,就是通過一個人的姓名查詢這個人的詳細信息。

ps:servlet是java web的基礎,理解servlet對理解整個java web非常重要,沒寫過servlet就開始用各種框架寫接口就是在胡鬧。

1. wsdl文件

準備以下wsdl文件,不要管這個文件是怎么來的,是怎么生成的,我們這次只講原理,不談細節,總之,你根據需求寫出了這個wsdl文件。

<?xml version="1.0" encoding="UTF-8" standalone="no"?><wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:sch="http://www.definesys.com/xml/employee" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.definesys.com/xml/employee" targetNamespace="http://www.definesys.com/xml/employee">
 <wsdl:types>
  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://www.definesys.com/xml/employee">

  <xs:element name="EmployeeDetailRequest">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="name" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:element name="EmployeeDetailResponse">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Employee" type="tns:Employee"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:complexType name="Employee">
    <xs:sequence>
      <xs:element name="name" type="xs:string"/>
      <xs:element name="email" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>

</xs:schema>
 </wsdl:types>
 <wsdl:message name="EmployeeDetailRequest">
  <wsdl:part element="tns:EmployeeDetailRequest" name="EmployeeDetailRequest">
  </wsdl:part>
 </wsdl:message>
 <wsdl:message name="EmployeeDetailResponse">
  <wsdl:part element="tns:EmployeeDetailResponse" name="EmployeeDetailResponse">
  </wsdl:part>
 </wsdl:message>
 <wsdl:portType name="Employee">
  <wsdl:operation name="EmployeeDetail">
   <wsdl:input message="tns:EmployeeDetailRequest" name="EmployeeDetailRequest">
  </wsdl:input>
   <wsdl:output message="tns:EmployeeDetailResponse" name="EmployeeDetailResponse">
  </wsdl:output>
  </wsdl:operation>
 </wsdl:portType>
 <wsdl:binding name="EmployeeSoap11" type="tns:Employee">
  <soap:binding  transport="http://schemas.xmlsoap.org/soap/http"/>
  <wsdl:operation name="EmployeeDetail">
   <soap:operation soapAction=""/>
   <wsdl:input name="EmployeeDetailRequest">
    <soap:body use="literal"/>
   </wsdl:input>
   <wsdl:output name="EmployeeDetailResponse">
    <soap:body use="literal"/>
   </wsdl:output>
  </wsdl:operation>
 </wsdl:binding>
 <wsdl:service name="EmployeeService">
  <wsdl:port binding="tns:EmployeeSoap11" name="EmployeeSoap11">
   <soap:address location="http://localhost:8081/ws-servlet/ws/employee-detail"/>
  </wsdl:port>
 </wsdl:service>
</wsdl:definitions>

soap:address location里面端口號需要修改為servlet運行的端口號。

從以下xml片段可以看出

...
<wsdl:binding name="EmployeeSoap11" type="tns:Employee">
  <soap:binding  transport="http://schemas.xmlsoap.org/soap/http"/>
  <wsdl:operation name="EmployeeDetail">
   <soap:operation soapAction=""/>
   <wsdl:input name="EmployeeDetailRequest">
    <soap:body use="literal"/>
   </wsdl:input>
   <wsdl:output name="EmployeeDetailResponse">
    <soap:body use="literal"/>
   </wsdl:output>
  </wsdl:operation>
 </wsdl:binding>
 <wsdl:service name="EmployeeService">
  <wsdl:port binding="tns:EmployeeSoap11" name="EmployeeSoap11">
   <soap:address location="http://localhost:8081/ws-servlet/ws/employee-detail"/>
  </wsdl:port>
 </wsdl:service>
  • 接口名稱是EmployeeDetail(wsdl:operation)
  • 接口輸入參數是EmployeeDetailRequest(wsdl:input)
  • 接口輸出參數是EmployeeDetailResponse(wsdl:output)
  • 接口地址是http://localhost:8081/ws-servlet/ws/employee-detail(soap:address)

2. 獲取wsdl文件servlet

package com.definesys.demo.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Copyright: Shanghai Definesys Company.All rights reserved.
 * @Description:
 * @author: jianfeng.zheng
 * @since: 2019/1/5 下午1:45
 * @history: 1.2019/1/5 created by jianfeng.zheng
 */
public class WsdlServlet extends HttpServlet {
  public static final String WSDL_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><wsdl:definitions xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\" xmlns:sch=\"http://www.definesys.com/xml/employee\" xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\" xmlns:tns=\"http://www.definesys.com/xml/employee\" targetNamespace=\"http://www.definesys.com/xml/employee\">\n" +
      " <wsdl:types>\n" +
      "  <xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" elementFormDefault=\"qualified\" targetNamespace=\"http://www.definesys.com/xml/employee\">\n" +
      "\n" +
      "  <xs:element name=\"EmployeeDetailRequest\">\n" +
      "    <xs:complexType>\n" +
      "      <xs:sequence>\n" +
      "        <xs:element name=\"name\" type=\"xs:string\"/>\n" +
      "      </xs:sequence>\n" +
      "    </xs:complexType>\n" +
      "  </xs:element>\n" +
      "\n" +
      "  <xs:element name=\"EmployeeDetailResponse\">\n" +
      "    <xs:complexType>\n" +
      "      <xs:sequence>\n" +
      "        <xs:element name=\"Employee\" type=\"tns:Employee\"/>\n" +
      "      </xs:sequence>\n" +
      "    </xs:complexType>\n" +
      "  </xs:element>\n" +
      "\n" +
      "  <xs:complexType name=\"Employee\">\n" +
      "    <xs:sequence>\n" +
      "      <xs:element name=\"name\" type=\"xs:string\"/>\n" +
      "      <xs:element name=\"email\" type=\"xs:string\"/>\n" +
      "    </xs:sequence>\n" +
      "  </xs:complexType>\n" +
      "\n" +
      "</xs:schema>\n" +
      " </wsdl:types>\n" +
      " <wsdl:message name=\"EmployeeDetailRequest\">\n" +
      "  <wsdl:part element=\"tns:EmployeeDetailRequest\" name=\"EmployeeDetailRequest\">\n" +
      "  </wsdl:part>\n" +
      " </wsdl:message>\n" +
      " <wsdl:message name=\"EmployeeDetailResponse\">\n" +
      "  <wsdl:part element=\"tns:EmployeeDetailResponse\" name=\"EmployeeDetailResponse\">\n" +
      "  </wsdl:part>\n" +
      " </wsdl:message>\n" +
      " <wsdl:portType name=\"Employee\">\n" +
      "  <wsdl:operation name=\"EmployeeDetail\">\n" +
      "   <wsdl:input message=\"tns:EmployeeDetailRequest\" name=\"EmployeeDetailRequest\">\n" +
      "  </wsdl:input>\n" +
      "   <wsdl:output message=\"tns:EmployeeDetailResponse\" name=\"EmployeeDetailResponse\">\n" +
      "  </wsdl:output>\n" +
      "  </wsdl:operation>\n" +
      " </wsdl:portType>\n" +
      " <wsdl:binding name=\"EmployeeSoap11\" type=\"tns:Employee\">\n" +
      "  <soap:binding style=\"document\" transport=\"http://schemas.xmlsoap.org/soap/http\"/>\n" +
      "  <wsdl:operation name=\"EmployeeDetail\">\n" +
      "   <soap:operation soapAction=\"\"/>\n" +
      "   <wsdl:input name=\"EmployeeDetailRequest\">\n" +
      "    <soap:body use=\"literal\"/>\n" +
      "   </wsdl:input>\n" +
      "   <wsdl:output name=\"EmployeeDetailResponse\">\n" +
      "    <soap:body use=\"literal\"/>\n" +
      "   </wsdl:output>\n" +
      "  </wsdl:operation>\n" +
      " </wsdl:binding>\n" +
      " <wsdl:service name=\"EmployeeService\">\n" +
      "  <wsdl:port binding=\"tns:EmployeeSoap11\" name=\"EmployeeSoap11\">\n" +
      "   <soap:address location=\"http://localhost:8081/ws-servlet/ws/employee-detail\"/>\n" +
      "  </wsdl:port>\n" +
      " </wsdl:service>\n" +
      "</wsdl:definitions>";

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setContentType("text/xml");
    resp.getOutputStream().write(WSDL_XML.getBytes());
  }
}

是不是很簡單,是的,為了簡單,我直接將wsdl文件用變量存儲,我們還需要配置下web.xml

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     version="3.1">
  <servlet>
    <servlet-name>wsdl</servlet-name>
    <servlet-class>com.definesys.demo.servlet.WsdlServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>wsdl</servlet-name>
    <url-pattern>/ws/employee</url-pattern>
  </servlet-mapping>
</web-app>

這樣我們訪問http://localhost:8080/ws/employee就能返回一個wsdl文件,也就是接口描述文件。在wsdl文件里,我們定義接口地址為http://localhost:8080/ws/employee-detail,接下來我們就要實現這個接口。

3. 業務servlet

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Copyright: Shanghai Definesys Company.All rights reserved.
 * @Description:
 * @author: jianfeng.zheng
 * @since: 2019/1/5 下午2:56
 * @history: 1.2019/1/5 created by jianfeng.zheng
 */
public class EmployeeServlet extends HttpServlet {
  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String response = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
        "  <SOAP-ENV:Header/>\n" +
        "  <SOAP-ENV:Body>\n" +
        "   <ns2:EmployeeDetailResponse xmlns:ns2=\"http://www.definesys.com/xml/employee\">\n" +
        "     <ns2:Employee>\n" +
        "      <ns2:name>jianfeng</ns2:name>\n" +
        "      <ns2:email>jianfeng.zheng@definesys.com</ns2:email>\n" +
        "     </ns2:Employee>\n" +
        "   </ns2:EmployeeDetailResponse>\n" +
        "  </SOAP-ENV:Body>\n" +
        "</SOAP-ENV:Envelope>";
    resp.getOutputStream().write(response.getBytes());
  }
}

這里不做任何業務處理,不做xml轉bean,不做bean轉xml,就是這么暴力,直接返回xml,但他仍是一個soap服務,支持所有soap工具調用。

將servlet配置到web.xml里

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     version="3.1">
  <servlet>
    <servlet-name>wsdl</servlet-name>
    <servlet-class>com.definesys.demo.servlet.WsdlServlet</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>employee</servlet-name>
    <servlet-class>com.definesys.demo.servlet.EmployeeServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>wsdl</servlet-name>
    <url-pattern>/ws/employee</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>employee</servlet-name>
    <url-pattern>/ws/employee-detail</url-pattern>
  </servlet-mapping>
</web-app>

/ws/employee-detail這個地址必須和wsdl文件里定義的保持一致,不然服務無法被找到。

4. 測試

使用soapui測試我們的webservice,通過地址http://localhost:8081/ws-servlet/ws/employee導入wsdl文件,測試接口,返回我們在業務servlet里面寫死的內容。恭喜你,你已經不依賴任何第三方包完成了一個soap webservice。

當然這個只是一個玩具,但框架就是在上面的基礎上進行擴展,增加wsdl文件自動生成,xml轉java,java轉xml,xml校驗,錯誤處理等功能,如果你有時間,你也可以寫一個soap webservice框架。

代碼已經上傳至github,歡迎star,開始進入正題,偏的有點遠。

spring boot開發soap webservice

1. 創建spring boot工程

你可以通過spring initializr初始化spring boot工程,也可以通過inte idea的spring initializr插件進行初始化,個人推薦后面這種。

2. 添加依賴

添加soap webservice相關依賴包和插件,

pom.xml

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

<dependency>
  <groupId>wsdl4j</groupId>
  <artifactId>wsdl4j</artifactId>
</dependency>
...

<!--插件-->
<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>
    <!--<schemaFiles>employee.xsd</schemaFiles>-->
    <outputDirectory>${project.basedir}/src/main/java</outputDirectory>
    <packageName>com.definesys.tutorial.ws.type</packageName>
    <clearOutputDir>false</clearOutputDir>
  </configuration>
</plugin>

插件jaxb2能夠實現java和xml之間互轉,下面是幾個參數的說明

  • schemaDirectory:xsd文件目錄
  • schemaFiles:指定schemaDirectory下的xsd文件,多個用逗號隔開,必須指定schemaDirectory
  • outputDirectory:生成java文件保存目錄
  • packageName:生成java文件包路徑
  • clearOutputDir:重新生成前是否需要清空目錄

3. 編寫xsd文件

假設我們的需求是通過員工工號查詢員工詳細信息,根據需求編寫以下xsd文件,并保存在/src/main/resources/目錄下。

employee.xsd

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.definesys.com/xml/employee"
      targetNamespace="http://www.definesys.com/xml/employee" elementFormDefault="qualified">

  <xs:element name="EmployeeDetailRequest">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="code" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:element name="EmployeeDetailResponse">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Employee" type="tns:Employee"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:complexType name="Employee">
    <xs:sequence>
      <xs:element name="code" type="xs:string"/>
      <xs:element name="name" type="xs:string"/>
      <xs:element name="email" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>

</xs:schema>

4. 生成java類型文件

我們需要根據xsd文件生成java類型文件,這就要借助maven插件jaxb2,打開終端運行命令mvn jaxb2:xjc,如果運行正常,就會在目錄com.definesys.tutorial.ws.type下生成一堆java文件,此時文件結構如下:

.
├── java
│  └── com
│    └── definesys
│      └── tutorial
│        └── ws
│          ├── SpringbootWsApplication.java
│          └── type
│            ├── Employee.java
│            ├── EmployeeDetailRequest.java
│            ├── EmployeeDetailResponse.java
│            ├── ObjectFactory.java
│            └── package-info.java
└── resources
  ├── application.properties
  ├── employee.xsd
  ├── static
  └── templates

5. 創建配置文件

WebserviceConfig.java

package com.definesys.tutorial.ws;

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.ws.wsdl.wsdl11.Wsdl11Definition;
import org.springframework.xml.xsd.SimpleXsdSchema;
import org.springframework.xml.xsd.XsdSchema;

/**
 * @Copyright: Shanghai Definesys Company.All rights reserved.
 * @Description:
 * @author: jianfeng.zheng
 * @since: 2019/1/5 下午4:46
 * @history: 1.2019/1/5 created by jianfeng.zheng
 */

@EnableWs
@Configuration
public class WebserviceConfig extends WsConfigurerAdapter {

  @Bean
  public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
    MessageDispatcherServlet servlet = new MessageDispatcherServlet();
    servlet.setApplicationContext(applicationContext);
    servlet.setTransformWsdlLocations(true);
    return new ServletRegistrationBean(servlet, "/ws/*");
  }

  @Bean(name = "employee")
  public Wsdl11Definition defaultWsdl11Definition(XsdSchema schema) {
    DefaultWsdl11Definition wsdl = new DefaultWsdl11Definition();
    wsdl.setPortTypeName("EmployeePort");
    wsdl.setLocationUri("/ws/employee-detail");
    wsdl.setTargetNamespace("http://www.definesys.com/xml/employee");
    wsdl.setSchema(schema);
    return wsdl;
  }

  @Bean
  public XsdSchema employeeSchema() {
    return new SimpleXsdSchema(new ClassPathResource("employee.xsd"));
  }
}

6. 創建業務服務

EmployeeSoapController.java

package com.definesys.tutorial.ws;

import com.definesys.tutorial.ws.type.Employee;
import com.definesys.tutorial.ws.type.EmployeeDetailRequest;
import com.definesys.tutorial.ws.type.EmployeeDetailResponse;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

/**
 * @Copyright: Shanghai Definesys Company.All rights reserved.
 * @Description:
 * @author: jianfeng.zheng
 * @since: 2019/1/5 下午4:49
 * @history: 1.2019/1/5 created by jianfeng.zheng
 */
@Endpoint
public class EmployeeSoapController {

  private static final String NAMESPACE_URI = "http://www.definesys.com/xml/employee";

  @PayloadRoot(namespace = NAMESPACE_URI, localPart = "EmployeeDetailRequest")
  @ResponsePayload
  public EmployeeDetailResponse getEmployee(@RequestPayload EmployeeDetailRequest request) {

    EmployeeDetailResponse response = new EmployeeDetailResponse();

    //這里只作為演示,真正開發中需要編寫業務邏輯代碼
    Employee employee = new Employee();
    employee.setName("jianfeng");
    employee.setEmail("jianfeng.zheng@definesys.com");
    employee.setCode(request.getCode());
    response.setEmployee(employee);

    return response;
  }
}

與RestController不一樣的是,spring boot soap是根據請求報文來指定調用的函數,RestController是根據請求路徑來確定。@PayloadRoot就是關鍵,如本次請求報文如下:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:emp="http://www.definesys.com/xml/employee">
  <soapenv:Header/>
  <soapenv:Body>
   <emp:EmployeeDetailRequest>
     <emp:code>?</emp:code>
   </emp:EmployeeDetailRequest>
  </soapenv:Body>
</soapenv:Envelope>

xmlns:emp="http://www.definesys.com/xml/employee"就是@PayloadRoot.namespace,emp:EmployeeDetailRequest對應@PayloadRoot.localPart。理解了這個其他都很好理解。

7. 測試

使用soapui進行測試,通過地址http://localhost:8080/ws/employee.wsdl導入wsdl文件進行測試。

輸入報文

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:emp="http://www.definesys.com/xml/employee">
  <soapenv:Header/>
  <soapenv:Body>
   <emp:EmployeeDetailRequest>
     <emp:code>004</emp:code>
   </emp:EmployeeDetailRequest>
  </soapenv:Body>
</soapenv:Envelope>

輸出報文

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP-ENV:Header/>
  <SOAP-ENV:Body>
   <ns2:EmployeeDetailResponse xmlns:ns2="http://www.definesys.com/xml/employee">
     <ns2:Employee>
      <ns2:code>004</ns2:code>
      <ns2:name>jianfeng</ns2:name>
      <ns2:email>jianfeng.zheng@definesys.com</ns2:email>
     </ns2:Employee>
   </ns2:EmployeeDetailResponse>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

同時提供soap和restful兩種服務

soap一般在企業內部用的比較多,做系統間的集成,restful一般用于移動應用和h6應用,如果在企業應用開發里能夠同時提供兩種協議的支持,將極大提高接口的復用。其實也沒有想象中的那么復雜,在本例中,只需把業務邏輯部分用service實現再創建一個RestController即可,通過設計模式即可解決,不需要引入新的技術。

EmployeeService.java

package com.definesys.tutorial.ws;

import com.definesys.tutorial.ws.type.Employee;
import com.definesys.tutorial.ws.type.EmployeeDetailRequest;
import com.definesys.tutorial.ws.type.EmployeeDetailResponse;
import org.springframework.stereotype.Service;

/**
 * @Copyright: Shanghai Definesys Company.All rights reserved.
 * @Description:
 * @author: jianfeng.zheng
 * @since: 2019/1/5 下午5:42
 * @history: 1.2019/1/5 created by jianfeng.zheng
 */
@Service
public class EmployeeService {

  public EmployeeDetailResponse getEmployee(EmployeeDetailRequest request) {

    EmployeeDetailResponse response = new EmployeeDetailResponse();

    //這里只作為演示,真正開發中需要編寫業務邏輯代碼
    Employee employee = new Employee();
    employee.setName("jianfeng");
    employee.setEmail("jianfeng.zheng@definesys.com");
    employee.setCode(request.getCode());
    response.setEmployee(employee);

    return response;
  }
}

EmployeeSoapController.java

package com.definesys.tutorial.ws;

import com.definesys.tutorial.ws.type.Employee;
import com.definesys.tutorial.ws.type.EmployeeDetailRequest;
import com.definesys.tutorial.ws.type.EmployeeDetailResponse;
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;

/**
 * @Copyright: Shanghai Definesys Company.All rights reserved.
 * @Description:
 * @author: jianfeng.zheng
 * @since: 2019/1/5 下午4:49
 * @history: 1.2019/1/5 created by jianfeng.zheng
 */
@Endpoint
public class EmployeeSoapController {

  @Autowired
  private EmployeeService service;

  private static final String NAMESPACE_URI = "http://www.definesys.com/xml/employee";

  @PayloadRoot(namespace = NAMESPACE_URI, localPart = "EmployeeDetailRequest")
  @ResponsePayload
  public EmployeeDetailResponse getEmployee(@RequestPayload EmployeeDetailRequest request) {

    return service.getEmployee(request);
  }
}

EmployeeRestController.java

package com.definesys.tutorial.ws;

import com.definesys.tutorial.ws.type.EmployeeDetailRequest;
import com.definesys.tutorial.ws.type.EmployeeDetailResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Copyright: Shanghai Definesys Company.All rights reserved.
 * @Description:
 * @author: jianfeng.zheng
 * @since: 2019/1/5 下午5:43
 * @history: 1.2019/1/5 created by jianfeng.zheng
 */
@RestController
@RequestMapping(value = "/rest")
public class EmployeeRestController {

  @Autowired
  private EmployeeService service;

  @RequestMapping(value = "/employee-detail", method = RequestMethod.POST)
  public EmployeeDetailResponse getEmployeeDetail(@RequestBody EmployeeDetailRequest request) {
    return service.getEmployee(request);
  }
}

測試

$ curl http://localhost:8080/rest/employee-detail -X POST -d '{"code":"004"}' -H "Content-Type: application/json"
{
  "employee": {
    "code": "004",
    "name": "jianfeng",
    "email": "jianfeng.zheng@definesys.com"
  }
}

這樣就實現了soap和rest同時提供的目的。

本文代碼已提交至gitlab歡迎star

相關參考文檔

https://spring.io/guides/gs/producing-web-service/
https://github.com/wls1036/tutorial-springboot-soap
https://github.com/wls1036/pure-ws-servlet

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

莎车县| 天水市| 福清市| 西城区| 郎溪县| 博客| 宁远县| 万荣县| 靖远县| 溆浦县| 隆化县| 双牌县| 毕节市| 麟游县| 洞头县| 霍山县| 洛阳市| 勐海县| 新源县| 威信县| 合作市| 晴隆县| 广南县| 渭南市| 历史| 正宁县| 阿克苏市| 湄潭县| 汝阳县| 双鸭山市| 安新县| 石景山区| 九龙县| 崇阳县| 右玉县| 柳江县| 商都县| 新野县| 汾阳市| 云浮市| 榆林市|