spring boot 开发soap webservice的实现代码

时间:2021-12-04 23:27:21

介绍

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文件。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?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 style="document" 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片段可以看出

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
...
<wsdl:binding name="employeesoap11" type="tns:employee">
  <soap:binding style="document" 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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<!--依赖-->
<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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<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文件,此时文件结构如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
.
├── 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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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就是关键,如本次请求报文如下:

?
1
2
3
4
5
6
7
8
<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文件进行测试。

输入报文

?
1
2
3
4
5
6
7
8
<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>

输出报文

?
1
2
3
4
5
6
7
8
9
10
11
12
<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一般用于移动应用和h5应用,如果在企业应用开发里能够同时提供两种协议的支持,将极大提高接口的复用。其实也没有想象中的那么复杂,在本例中,只需把业务逻辑部分用service实现再创建一个restcontroller即可,通过设计模式即可解决,不需要引入新的技术。

employeeservice.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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);
  }
}

测试

?
1
2
3
4
5
6
7
8
$ 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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://segmentfault.com/a/1190000017777211