spring boot Development soap webservice

Introduction
spring boot web module provides time RestController achieve restful, first saw the name of that there SoapController, unfortunately not, for soap webservice provided another module spring-boot-starter-web- services support. This article describes how to develop soap webservice interface spring boot and how to interface to simultaneously support both protocols soap and restful. 
soap webservice 
Web Service is an independent, low coupling a platform, self-contained, programmable web-based application that can be either soap webservice can be rest webservice, before the rest has not come out, we usually say webservice It refers to a web application based on the communication protocol soap.  
Before we begin, I feel the need to understand the next soap webservice, specifically the concept of the Internet can find a lot of information, but a strong conceptual information online, and soap is the protocol used to communicate xml, xml inside a namespace believe will be able to scare a piles of people, so here not to discuss the specific details of the agreement soap, I want an example to illustrate what is soap webservice, by this example, soap webservice you understand how it works, of course, if you feel that you already know the , you can skip this section, this section has nothing to do with the back of the content. 

Suppose we have developed a web interface, others want to use, how we want to do 
   
 1. Deploy an interface to the server    
 2. Write the interface documentation, the interface is written clearly through what method calls and what input parameters are, what the output parameter is wrong What return. 
That question is, can we just put the interface deployed to the server, then the interface will not only provide specific services, but also automatically generates a standard interface documentation, the interface information are recorded in the document, if do that is not able to achieve the purpose, "namely the interface documentation" of.  
So one interface, including what information it?
     1. Interface Address    
     2. The method of interface calls    
     3. The interface input parameter    
     4. Interface output parameters    
     The interfaces return an error message    
     . 6 ..... 
    
SOAP WebService wsdl file is in the interface description. The core message is that more than a few.

The second problem, because the Web service is a platform-independent, that is, using the interface of people do not know what this service is technology development, may be likely to be php java, etc., but the parameters of the interface and the data is returned the same, to achieve this goal, we need two things, one is with the platform-independent data format, soap using xml, a communication protocol, which is the soap protocol. 

Here's how not to use any framework, only to achieve a webservice through a servlet. The webservice function is very simple, the details of a person is a query by a person's name.
 
ps: servlet is the basis for java web, java web servlet whole is very important to understand understanding, never wrote servlet began to write interfaces with a variety of frame that is nonsense.
 
 1. wsdl file the following wsdl file, do not cover this document is how come, how is generated, this time we talk about principles, not talk about details, in short, you write the wsdl file on demand.

spring boot Development soap webservice

spring boot Development soap webservice
spring boot Development soap webservice
soap: address location inside the port numbers need to change the port number servlet running.  
As can be seen from the following xml fragments   
spring boot Development soap webservice
interface name is EmployeeDetail (wsdl: operation)    
interface input parameters EmployeeDetailRequest (wsdl: input)    
interface output parameter is EmployeeDetailResponse (wsdl: output)    
interface address is http: // localhost: 8081 / ws -servlet / ws / employee-detail (soap : address)
spring boot Development soap webservice

spring boot Development soap webservice

spring boot Development soap webservice

spring boot Development soap webservice

Is not very simple, yes, for simplicity, I direct the wsdl file with variable storage, we also need to configure the next web.xml
spring boot Development soap webservice

We visit http: // localhost: 8080 / ws / employee will be able to return to a wsdl file, which is the interface description. In the wsdl file, we define the interface address is http: // localhost: 8080 / ws / employee-detail, then we will implement this interface.

  1. 业务servlet
    这里不做任何业务处理,不做xml转bean,不做bean转xml,就是这么暴力,直接返回xml,但他仍是一个soap服务,支持所有soap工具调用。 
    spring boot Development soap webservice
    spring boot Development soap webservice
    将servlet配置到web.xml里

web.xml
spring boot Development soap webservice
这个地址必须和wsdl文件里定义的保持一致,不然服务无法被找到。 

  1. 测试 使用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
spring boot Development soap webservice
spring boot Development soap webservice

插件jaxb2能够实现java和xml之间互转,下面是几个参数的说明 
    schemaDirectory:xsd文件目录    
schemaFiles:指定schemaDirectory下的xsd文件,多个用逗号隔开,必须指定  schemaDirectory    
outputDirectory:生成java文件保存目录    
packageName:生成java文件包路径    
clearOutputDir:重新生成前是否需要清空目录 

  1. 编写xsd文件 假设我们的需求是通过员工工号查询员工详细信息,根据需求编写以下xsd文件,并保存在/src/main/resources/目录下。  
    employee.xsd
    spring boot Development soap webservice
    spring boot Development soap webservice
  2. 生成java类型文件 
    我们需要根据xsd文件生成java类型文件,这就要借助maven插件jaxb2,打开终端运行命令mvn jaxb2:xjc,如果运行正常,就会在目录com.definesys.tutorial.ws.type下生成一堆java文件,此时文件结构如下:
    spring boot Development soap webservice
  3. 创建配置文件 
    WebserviceConfig.java
    spring boot Development soap webservice
    spring boot Development soap webservice
    spring boot Development soap webservice
  4. 创建业务服务 

    EmployeeSoapController.java

    spring boot Development soap webservice
    spring boot Development soap webservice
    与RestController不一样的是,spring boot soap是根据请求报文来指定调用的函数,RestController是根据请求路径来确定。@PayloadRoot就是关键,如本次请求报文如下:
    spring boot Development soap webservice
    xmlns:emp="http://www.definesys.com/xml/employee"就是@PayloadRoot.namespace,emp:EmployeeDetailRequest对应@PayloadRoot.localPart。理解了这个其他都很好理解

  5. Testing 
    using soapui test, through the address http: // localhost: 8080 / ws / employee.wsdl import wsdl file for testing. 
    Input packets
    spring boot Development soap webservice
    and outgoing packets
    spring boot Development soap webservice
    while providing soap and restful two services 
    soap is generally used within the enterprise are more integrated, restful among the general system for mobile applications do h5 application and, if we can also provide application development in the enterprise in two support kinds of protocol, will greatly enhance the multiplexing interface. In fact, not quite that complicated, in this case, only the business logic with the service and then create a RestController to achieve, can be solved by design patterns, no need to introduce new technologies.  
    EmployeeService.java
    spring boot Development soap webservice
    spring boot Development soap webservice
    EmployeeSoapController.java
    spring boot Development soap webservice
    spring boot Development soap webservice
    EmployeeRestController.java
    spring boot Development soap webservice
    spring boot Development soap webservice
    test
    spring boot Development soap webservice
    so to achieve the purpose of soap and rest at the same time provide.
    I venture team product MadPecker, mainly to do BUG management, test management, application distribution
    URL: www.madpecker.com, a friend in need are welcome to try, to experience!
    This article MadPecker technical team to write, reproduced, please indicate the source

Reproduced in: https: //blog.51cto.com/14322715/2410976

Guess you like

Origin blog.csdn.net/weixin_33975951/article/details/93036186