如何在Spring Boot应用程序中配置了两个不同的SOAP Web服务端点
作者:weixin_37370093
这篇文章主要介绍了如何在Spring Boot应用程序中配置了两个不同的SOAP Web服务端点,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
在Spring Boot应用程序中配置了两个不同的SOAP Web服务端点
新建一个CustomMessageDispatcherServlet类,以扩展以下MessageDispatcherServlet类,
import org.springframework.ws.transport.http.MessageDispatcherServlet;
import javax.servlet.http.HttpServletRequest;
public class CustomMessageDispatcherServlet extends MessageDispatcherServlet {
@Override
protected String transformWsdlLocation(HttpServletRequest request, String wsdlLocation) {
String requestUri = request.getRequestURI();
if (requestUri.contains("/WebservicesConnector/services/countries")) {
return "countries.wsdl";
} else if (requestUri.contains("/FinancingService/FinancingUpdate.asmx")) {
return "kinsai.wsdl";
}
return super.transformWsdlLocation(request, wsdlLocation);
}
}messageDispatcherServlet
@Bean
public ServletRegistrationBean<CustomMessageDispatcherServlet> messageDispatcherServlet() {
CustomMessageDispatcherServlet servlet = new CustomMessageDispatcherServlet();
servlet.setApplicationContext(applicationContext);
servlet.setTransformWsdlLocations(true);
return new ServletRegistrationBean<>(servlet, "/ws/*");
}扩展:基于SpringBoot 的SOAP WebService实现
一、使用postman工具调用服务接口
成功启动springboot应用后,使用postman新建POST请求,地址: http://localhost:8080/soap/userManagement
正文body选择raw,XML格式。
headers填入如下键值对:

其中xlms字段是 WSDL中的namespace字段。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.tmy.example.org/">
<soapenv:Header/>
<soapenv:Body>
<ser:getUserByName>
<name>Jerry</name>
</ser:getUserByName>
</soapenv:Body>
</soapenv:Envelope>发送请求,返回了一个User类 。
至此,webservice SOAP服务发布测试成功。

二、使用客户端测试接口
新建客户端模块,maven依赖和服务端相同。
实体类User、服务接口UserManagement.java和服务端保持一致。
客户端结构如下:

测试类如下:
@SpringBootApplication
public class WebserviceClientApplication {
public static void main(String[] args) {
SpringApplication.run(WebserviceClientApplication.class, args);
JaxWsDynamicClientFactory dcflient=JaxWsDynamicClientFactory.newInstance();
Client client=dcflient.createClient("http://localhost:8080/soap/userManagement?wsdl");//http://localhost:8080/soap/userManagement
System.out.println("client= "+client);
try{
//namespace= http://service.tmy.example.org/
QName opname=new QName("http://service.tmy.example.org/","getUserByName");
Object[] objects=client.invoke(opname,"Jerry");//getUserByName
System.out.println("getUserByName 调用结果:"+objects[0].toString());
}catch (Exception e){
e.printStackTrace();
}
}
}启动服务端后,运行客户端,返回了一个User类,说明客户端测试成功。

到此这篇关于在Spring Boot应用程序中配置了两个不同的SOAP Web服务端点的文章就介绍到这了,更多相关Spring Boot应用程序配置了两个不同的SOAP Web服务端点内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
