【问题标题】:Spring CXF Soap Web Service Under Tomcat: No Services FoundTomcat下的Spring CXF Soap Web服务:未找到服务
【发布时间】:2017-12-12 00:54:28
【问题描述】:

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

我有一个 Web 应用程序初始化程序来引导 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 配置类:

@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 服务实现:

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

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

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

没有找到服务。

我可以让它工作的唯一方法是发布如下,这似乎将它发布到嵌入式 Jetty 服务器而不是它部署到的 Tomcat 实例:

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

为了让它在 Tomcat 上运行,我缺少什么:

endpoint.publish("/EorthoWebService");

【问题讨论】:

    标签: java spring cxf


    【解决方案1】:

    您缺少的部分是弹簧上下文扫描。

    来自Baeldung——A Guide to Apache CXF with Spring

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

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

    ServiceConfiguration 类使用@Configuration 注解进行注解,以提供 bean 定义。此类将在下一小节中讨论。 以下 sn-p 显示了如何将 Spring 应用程序上下文添加到 servlet 上下文中:

    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 时,它似乎也能正常工作。希望这足以让您解决问题。

    另外,我的配置类:

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

    【讨论】:

    • 谢谢卡尔。我还发现根本不创建 SpringBus bean 并调用 EndpointImpl endpoint = new EndpointImpl(new HelloWorldImpl()); 也解决了问题。
    • 嗯,很有趣。没试过。谢谢。
    • 就我个人而言,在构造 EndpointImpl 时省略了 SpringBus 参数是导致错误的原因。该服务注册在一个 SpringBus 上,而另一个 SpringBus(bean 之一)实际上处理了 HTTP 请求并且没有找到任何注册的服务。
    猜你喜欢
    • 1970-01-01
    • 2019-08-05
    • 2012-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-31
    相关资源
    最近更新 更多