Ganesh Gudghe
Ganesh Gudghe

Reputation: 1387

Apache CXF how to deploy/run SOAP service on tomcat insted of in build jetty server

How to deploy CXF SOAP web service on tomcat instead of jetty server if i try to use tomcat port to run ServerFactoryBean it is showing an error:

port already running.

Is there any way to use tomcat instead of in-build jetty server?

Following is my code that i am trying to create a Server.

SoapBindingFactory bindingFactory = new SoapBindingFactory();
Bus bus = BusFactory.newInstance().createBus();
bindingFactory.setBus(bus);
bus.getExtension(BindingFactoryManager.class).registerBindingFactory("http://schemas.xmlsoap.org/wsdl/soap/", bindingFactory);
bus.getExtension(BindingFactoryManager.class).registerBindingFactory("http://schemas.xmlsoap.org/wsdl/soap/http", bindingFactory);
Service service = new WSDLServiceFactory(bus, "wsdl path here", null).create();

ServerFactoryBean serverFactory = new ServerFactoryBean();
serverFactory.setBus(bus);

InboundRMHttpInvoker invoker = new InboundRMHttpInvoker(serviceImpl);
serverFactory.setInvoker(invoker);

serverFactory.setServiceBean(serviceImpl);
serverFactory.setDataBinding(service.getDataBinding());
serverFactory.setServiceName(service.getName());
serverFactory.setBindingId(service.getServiceInfos().get(0).getBindings().iterator().next().getBindingId());
serverFactory.setWsdlLocation("wsdl path");
serverFactory.setEndpointName(service.getServiceInfos().iterator().next().getEndpoints().iterator().next().getName());
serverFactory.setAddress("http://localhost:8080/services/sampleservice");
Server server = serverFactory.create();

If I use another port (other than tomcat) then it deploy my service on that port but how to run it on tomcat port.

Upvotes: 0

Views: 488

Answers (1)

Alberto Pérez
Alberto Pérez

Reputation: 112

You can consider developing your application with Spring Boot or similar. When you deploy the war in the application container (eg tomcat), the ports configured in the container will be used.

Update

  1. Configure a main class.
@SpringBootApplication
public class AwesomeApp extends SpringBootServletInitializer {

    public static void main(String[] args) {
        new SpringApplicationBuilder(AwesomeApp.class)
                .run(args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(AwesomeApp.class);
    }
}
  1. Configure a CXF Servlet in @Configuration class.
@Configuration
public class ServletConfig implements WebMvcConfigurer {

    public ServletRegistrationBean<CXFServlet> cxfServletRegistration() {
        ServletRegistrationBean<CXFServlet> bean = new ServletRegistrationBean<>(
                new CXFServlet(), "/services/*");
        bean.setLoadOnStartup(1);
        return bean;
    }    
}
  1. Implements the service.

  2. Configure the Endpoint in @Configuration class and register the implementation.

@Configuration
public class IntegrationConfig {
    
    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    } 
    
    @Bean(name = "awesomeServiceImpl")
    public AwesomeServiceImpl awesomeServiceImpl()  {
        return new AwesomeServiceImpl();
    }
    
    @Bean
    public Endpoint awesomeEndpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), awesomeServiceImpl());
        endpoint.publish("/awesomeService");
        
        return endpoint;
    }   
}
  1. Run your app and browse to /services/awesomeService.

Upvotes: 1

Related Questions