Apache CXF如何在构建码头服务器中安装的tomcat上部署/运行SOAP服务

问题描述

如何在tomcat而非Jetty服务器上部署 CXF SOAP Web服务 如果我尝试使用tomcat端口运行ServerFactoryBean,则显示错误:

端口已在运行。

是否可以使用tomcat代替内置码头服务器?

以下是我尝试创建服务器的代码。

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();

如果我使用其他端口(tomcat除外),它将在该端口上部署我的服务,但是如何在tomcat端口上运行它。

解决方法

您可以考虑使用Spring Boot或类似程序开发应用程序。当您在应用程序容器(例如tomcat)中部署战争时,将使用容器中配置的端口。

更新

  1. 配置主类。
@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. @Configuration类中配置CXF Servlet。
@Configuration
public class ServletConfig implements WebMvcConfigurer {

    public ServletRegistrationBean<CXFServlet> cxfServletRegistration() {
        ServletRegistrationBean<CXFServlet> bean = new ServletRegistrationBean<>(
                new CXFServlet(),"/services/*");
        bean.setLoadOnStartup(1);
        return bean;
    }    
}
  1. 实施服务。

  2. @Configuration类中配置端点并注册实现。

@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. 运行您的应用并浏览到/services/awesomeService

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...