Search code examples
web-servicesspring-bootsoapcxf

Apache CXF + SpringBoot: Can I publish multiple endpoints for one SOAP web-service?


I have implemented a SOAP Webservice using Apache CXF + SpringBoot.

In my Endpoint Configuration class, I have

@Bean
public Endpoint endpoint()
{
    EndpointImpl endpoint = new EndpointImpl(cxfBus, new ServiceImpl());
    endpoint.publish("/myservice");
    return endpoint;
}

This creates a web-service endpoint as https://host:port/myService

To this service I need to expose multiple endpoints - something like - https://host:port/tenant1/myService
https://host:port/tenant2/myService
https://host:port/tenant3/myService

This is kind of REST endpoint -- i.e, I am trying to pass the tenantId variable in the service endpoint.

Is this possible in Apache CXF + Springboot ?

I tried this -

@Bean
public Endpoint endpoint()
{
    EndpointImpl endpoint = new EndpointImpl(cxfBus, new ServiceImpl()); 
    String[] pathArray = {"tenant1", "tenant2", "tenant3"};
    for (int i = 0; i < pathArray.length; i++)
    {
        endpoint.publish("/" + pathArray[i] + "/myservice");
    }
    return endpoint;
}

But it does not work.

I would really appreciate any inputs/suggestions. Thanks!


Solution

  • No you can not have same endpoint mapped to multiple urls, One Endpoint is create for a wsdl file which that would be generated to single class. From the urls I assume you want to host same service on multiple url based on tenant. In that case you have to create endpoints for each tenant.

    @Bean
    public Endpoint endpoint1()
    {
        EndpointImpl endpoint = new EndpointImpl(cxfBus, new ServiceImpl()); 
        endpoint.publish("/tenant1/" + pathArray[i] + "/myservice");
        return endpoint;
    }
    
    @Bean
    public Endpoint endpoint2()
    {
        EndpointImpl endpoint = new EndpointImpl(cxfBus, new ServiceImpl()); 
        endpoint.publish("/tenant1/" + pathArray[i] + "/myservice");
        return endpoint;
    }
    

    OR

    @Configuration
    public class CxfConfiguration implements BeanFactoryPostProcessor {
    
        @Override
        public void postProcessBeanFactory(ConfigurableListableBeanFactory factory) throws BeansException {
    
            Arrays.stream(new String[] { "tenant1", "tenant2" }).forEach(str -> {
                Bus bus = factory.getBean(Bus.class);
                JaxWsServerFactoryBean bean = new JaxWsServerFactoryBean();
                bean.setAddress("/" + str + "/myService");
                bean.setBus(bus);
                bean.setServiceClass(HelloWorld.class);
                factory.registerSingleton(str, bean.create());
            });
    
        }
    
    
    }
    

    BTW: May be better way use REST?