Tomcat下的Spring CXF Soap Web服务:未找到服务

Tomcat下的Spring CXF Soap Web服务:未找到服务

问题描述:

我正在尝试使用CXF和Spring设置在Tomcat上运行的简单CXF Web服务:

I'm trying to set-up a simple CXF Web Service running on Tomcat with CXF and Spring:

我有一个Web应用程序初始化程序来引导CXF servlet:

I have a Web Application initializer to bootstrap the CXF servlet:

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer
{
  @Override
  protected void registerContextLoaderListener(ServletContext servletContext)
  {
    CXFServlet cxfServlet = new CXFServlet();
    ServletRegistration.Dynamic dispatcher = servletContext.addServlet("cxf", cxfServlet);
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/services/*");
  }

  .....
}

我有一个Spring配置类:

I have a Spring configuration class:

@Configuration
public class WebServiceConfiguration
{
  @Bean
  public Endpoint endPoint()
  {
    EndpointImpl endpoint = new EndpointImpl(cxf(), eorthoWebService());
    endpoint.getHandlers().add(inboundRequestHandler());
    endpoint.getHandlers().add(outboundRequestHandler());
    //the below works and uses cxf's embedded Jetty server
    //endpoint.publish("http://localhost:9090/services/EorthoWebService");
    //this doesn't work
    endpoint.publish("/EorthoWebService");

    return endpoint;
  }

  @Bean
  public SpringBus cxf()
  {
    return new SpringBus();
  }

  @Bean
  public EorthoWebService eorthoWebService()
  {
    return new EorthoWebServiceImpl();
  }
}

我有一个Web服务实现:

I have a Web Service implementation:

@WebService(endpointInterface = "com.aoa.eortho.ws.service.EorthoWebService")
@SchemaValidation(type = SchemaValidationType.IN)
public class EorthoWebServiceImpl implements EorthoWebService {

    @WebMethod
    public RulesEngineOrthodonticSubmissionResponseEnv processRequest(RulesEngineOrthodonticSubmissionRequestEnv requestEnvelope) {
        ...
    }
}

当我点击/services时,我得到了输出:

When I hit /services I get the output:

未找到任何服务.

No services have been found.

我唯一可行的方法是通过如下所示的发布,该发布似乎是将其发布到嵌入式Jetty服务器,而不是将其部署到Tomcat实例:

The only way I can it to work is by publishing as below which seems to publish it to an embedded Jetty server rather than the Tomcat instance it is deployed to:

endpoint.publish("http://localhost:9090/services/EorthoWebService");

要使用以下命令在Tomcat上运行,我缺少什么?

What am I missing to get it working on Tomcat using:

endpoint.publish("/EorthoWebService");

您缺少的部分是spring contextscanning.

The part you're missing is the spring contextscanning.

来自 Baeldung -

首先,创建Spring应用程序上下文并将其配置为注册包含配置元数据的类:

First, a Spring application context is created and configured to register a class containing configuration metadata:

AnnotationConfigWebApplicationContext context 
    = new AnnotationConfigWebApplicationContext();
context.register(ServiceConfiguration.class);

使用@Configuration注释对ServiceConfiguration类进行注释,以提供Bean定义.下一节将讨论此类.以下代码片段显示了如何将Spring应用程序上下文添加到servlet上下文中:

The ServiceConfiguration class is annotated with the @Configuration annotation to provide bean definitions. This class is discussed in the next subsection. The following snippet shows how the Spring application context is added to the servlet context:

container.addListener(new ContextLoaderListener(context));

因此,完整的类是:

public class AppInitializer implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext container) {

        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.register(ServiceConfiguration.class);

        container.addListener(new ContextLoaderListener(context));

        // Register and map the dispatcher servlet
        ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new CXFServlet());
        dispatcher.setLoadOnStartup(1);

        dispatcher.addMapping("/services/*");
    }
}

与您的 AbstractAnnotationConfigDispatcherServletInitializer 扩展类不同,但是当我覆盖 onStartup 而不是 registerContextLoaderListener 时,它似乎也能正常工作.希望这足以让您梳理一下.

Which is different from your AbstractAnnotationConfigDispatcherServletInitializer extended class but when i override onStartup instead of registerContextLoaderListener it seems to work just as well. Hopefully this is enough to get you sorted out.

另外,我的配置类:

@Configuration
public class ServiceConfiguration {

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(cxf(), new HelloWorldImpl());
        endpoint.publish("/HelloWorld");
        return endpoint;
    }

    @Bean
    public SpringBus cxf() {
        return new SpringBus();
    }

}