使用Spring-ws 创建WebService服务端
一、 搭建环境
环境使用的是普通web项目
1、创建一个项目
![](https://i-blog.csdnimg.cn/blog_migrate/450bdf0d2094081a05b641cc0ee2979b.png)
2、需要使用的jar包
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yao</groupId>
<artifactId>webservice-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<finalName>test</finalName>
</build>
<properties>
<spring.version>4.3.2.RELEASE</spring.version>
<log4j2_version>2.5</log4j2_version>
<springws.version>2.4.0.RELEASE</springws.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- spring 依赖配置 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- webservice依赖配置 -->
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-core</artifactId>
<version>${springws.version}</version>
</dependency>
<!-- log4j依赖配置 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2_version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j2_version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>${log4j2_version}</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom2</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.5.1</version>
</dependency>
</dependencies>
</project>
3、配置web.xml
<servlet>
<servlet-name>spring-ws</servlet-name>
<servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
<!--Spring-WS配置文件的位置,默认寻找[servlet-name]-servlet.xml文件 -->
<!--<init-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/spring-ws-config.xml</param-value>
</init-param> -->
<init-param>
<param-name>transformWsdlLocations</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-ws</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
4、配置spring-ws配置文件
spring-ws-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sws="http://www.springframework.org/schema/web-services"
xmlns:ws="http://www.springframework.org/schema/integration/ws"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/web-services
http://www.springframework.org/schema/web-services/web-services-2.0.xsd">
<!-- 扫描指定包下的组件 -->
<context:component-scan base-package="com.yao" />
<sws:annotation-driven/>
<!-- 引入wsdl文件 -->
<sws:dynamic-wsdl id="login" portTypeName="LoginCheck" locationUri="/LoginCheck">
<sws:xsd location="classpath:Login.xsd"/>
</sws:dynamic-wsdl>
<sws:dynamic-wsdl id="test" portTypeName="test1" locationUri="testaa">
<sws:xsd location="classpath:test1.xsd"/>
</sws:dynamic-wsdl>
<!-- <sws:static-wsdl location="/WEB-INF/wsdl/UserService.wsdl" /> -->
</beans>
5、注意: portTypeName 和 locationUri
portTypeName :生成服务类的类名(本人自己的理解)
![](https://i-blog.csdnimg.cn/blog_migrate/2e746ff9fa835ecfa5dc693a72dc12d7.png)
![](https://i-blog.csdnimg.cn/blog_migrate/fc64479520e002c08e6a02e3aa6e8e4c.png)
locationUri:访问的生成的wsdl文件
地址 + 项目名 +
locationUri + id + .wsdl
![](https://i-blog.csdnimg.cn/blog_migrate/3011836a5c8cd76b350279138a56c9f3.png)
访问内容:
![](https://i-blog.csdnimg.cn/blog_migrate/199b2339de1528d437ffcf31087378c1.png)
注意: 这里需要注意路径区分大小写
locationUri="test" 和项目(test)名相同则,他们会重合如:
项目名为 test
访问项wsdl 则为
http://localhost:8080/test/test.wsdl
若
locationUri="test",访问路径也为
http://localhost:8080/test/test.wsdl,
若
locationUri="testaa", 访问路径则为
http://localhost:8080/test/testaa/test.wsdl。
二、 定义参数(.xsd文件)
1、test2.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://bb.yao.com" targetNamespace="http://bb.yao.com"
elementFormDefault="qualified">
<!--登录方法 -->
<xs:element name="TestBBRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="username" type="xs:string" />
<xs:element name="password" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<!--登录响应结果 -->
<xs:element name="TestBBResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="result" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
定义参数时需要注意:
A 、targetNamespace="
http://bb.yao.com" (http:// 包名倒过来)
B、 <xs:element name="TestBBRequest">
中的name 必须是 xxxRequest, 响应参数必须是 xxxResponse
不然wsdl文件中会没有如下定义,然后生产代码后,使用SoapUI测试时,你会发现不能输入参数
![](https://i-blog.csdnimg.cn/blog_migrate/9ba4833af42bb9f3fdaf98394b1926f0.png)
2、在 spring-ws-servlet.xml 中配置
![](https://i-blog.csdnimg.cn/blog_migrate/6302f3fec778904981d3c4750d1f85c9.png)
没有编写接口切点类或是名称空间(切点类中的namespace)不对
警告: No endpoint mapping found for [SaajSoapMessage {http://aa.yao.com}testInaaRequest]
解决web services - Spring ws: No adapter for endpoint
我出现No adapter for endpoint 的异常是因为我的.xsd文档定义错误导致的,如下图
![](https://i-blog.csdnimg.cn/blog_migrate/09ceca8f8d3aeb862d25bdcc4e3b8025.png)
将注释掉的代码改为了上面登陆了响应结果的代码就行了。
三、使用jdk生成代码
wsimport -encoding utf-8 -keep C:\Users\admin\Desktop\test http://localhost:8080/test/testBB/testbb.wsdl
wsimport -encoding utf-8 -keep 代码路径 wsdl地址
将生成代码复制进项目中
![](https://i-blog.csdnimg.cn/blog_migrate/cc8a3d8548bbe44491db75c1a24c65bc.png)
四、编写接口切点类
TestEndpoint.class
@Endpoint
public class TestEndpoint {
@PayloadRoot(namespace="http://aa.yao.com",localPart="testInaaRequest")
@ResponsePayload
public TestInaaResponse testABC(@RequestPayload TestInaaRequest request) {
TestInaaResponse t = new TestInaaResponse();
t.setResult(request.getUsername()+ request.getPassword());
return t;
}
}
五、测试接口(使用SoapUI工具)
![](https://i-blog.csdnimg.cn/blog_migrate/692cf8bdccbacadbba09bf4a758b42e4.png)