Apache CXF如何在Tomcat上部署/运行SOAP服务,而不是使用内置的Jetty服务器。

huangapple go评论74阅读模式
英文:

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

问题

如何在Tomcat上部署CXF SOAP Web服务,而不是Jetty服务器
如果我尝试使用Tomcat端口运行ServerFactoryBean,会显示一个错误:

> 端口已在运行。

是否有任何方法可以使用Tomcat而不是内置的Jetty服务器?

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

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路径在这里", 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路径");
serverFactory.setEndpointName(service.getServiceInfos().iterator().next().getEndpoints().iterator().next().getName());
serverFactory.setAddress("http://localhost:8080/services/sampleservice");
Server server = serverFactory.create();

如果我使用另一个端口(而不是Tomcat的端口),它会在该端口部署我的服务,但如何在Tomcat端口上运行它。

英文:

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.

答案1

得分: 1

你可以考虑使用Spring Boot或类似的方式来开发你的应用程序。当你在应用程序容器中部署WAR文件(例如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
英文:

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&lt;CXFServlet&gt; cxfServletRegistration() {
        ServletRegistrationBean&lt;CXFServlet&gt; bean = new ServletRegistrationBean&lt;&gt;(
                new CXFServlet(), &quot;/services/*&quot;);
        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 = &quot;awesomeServiceImpl&quot;)
	public AwesomeServiceImpl awesomeServiceImpl()  {
		return new AwesomeServiceImpl();
	}
	
	@Bean
	public Endpoint awesomeEndpoint() {
		EndpointImpl endpoint = new EndpointImpl(springBus(), awesomeServiceImpl());
		endpoint.publish(&quot;/awesomeService&quot;);
		
		return endpoint;
	}	
}
  1. Run your app and browse to /services/awesomeService.

huangapple
  • 本文由 发表于 2020年8月17日 12:45:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63444678.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定