首页 > 解决方案 > 为单个服务配置多个端点

问题描述

cxf-spring-boot-starter-jaxws用来编写一个简单的 SOAP 服务。WSDL 有一个服务和几个端口,看起来像这样

<wsdl:port name="myServiceSoap11Endpoint" binding="ns:myServiceSoap11Binding">
    <soap:address location="http://example.com/services/myService.myServiceSoap11Endpoint/"/>
</wsdl:port>
<wsdl:port name="myServiceSoap12Endpoint" binding="ns:myServiceSoap12Binding">
    <soap12:address location="http://example.com/services/myService.myServiceSoap12Endpoint/"/>
</wsdl:port>

两个绑定几乎相同,并指向相同的 PortType。

在我的 java 代码中,我使用 spring boot@Configuration机制配置端点。我为每个端口创建一个单独的端点。

// The class MyService was auto-generated by wsdl2java
@Bean
public Endpoint endpointMyServiceSoap11() {
    EndpointImpl endpoint = new EndpointImpl(springBus, new MyServiceImplementor());
    endpoint.setWsdlLocation(MyService.WSDL_LOCATION.toString());
    endpoint.setServiceName(MyService.SERVICE);
    endpoint.setEndpointName(MyService.MyServiceSoap11Endpoint);

    endpoint.publish("/myService.myServiceSoap11Endpoint");

    return endpoint;
}

@Bean
public Endpoint endpointMyServiceSoap12() {
    EndpointImpl endpoint = new EndpointImpl(springBus, new MyServiceImplementor());
    endpoint.setWsdlLocation(MyService.WSDL_LOCATION.toString());
    endpoint.setServiceName(MyService.SERVICE);
    endpoint.setEndpointName(MyService.MyServiceSoap12Endpoint);

    endpoint.publish("/myService.myServiceSoap12Endpoint");

    return endpoint;
}

这有点工作,但是当我想要获取 WSDL 文件时问题就开始了。两个端点都作为单独的服务发布,并且它们都提供自己的 WSDL 版本,每个端点只有一个端口是正确的。

有没有办法将两个端点作为公共服务的一部分发布,以便/myService?WSDL返回一个正确的 WSDL 与两个端点?

标签: javaspring-bootsoapwsdlcxf

解决方案


经过一番干预,我发现不可能做我想做的事。不过,至少有一种方法可以获取正确的 WSDL 文件。您可以通过使用 autoRewriteSoapAddressForAllServices 属性来做到这一点,例如像这样

endpoint.setProperties(Map.of(WSDLGetUtils.AUTO_REWRITE_ADDRESS_ALL, true))

但是,此属性不起作用cxf.path,您需要将所有内容放入server.servlet.contextPath中,因为 WSDL 中的结果路径基本上是通过将 contextPath 与端点发布路径结合起来创建的。


推荐阅读