我尝试使用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) {
...
}
}
当我点击/服务时,我得到输出:
未找到任何服务。
我可以使用它的唯一方法是发布如下所示,它似乎将它发布到嵌入式Jetty服务器而不是它部署到的Tomcat实例:
endpoint.publish("http://localhost:9090/services/EorthoWebService");
我错过了使用以下方法处理Tomcat:
endpoint.publish("/EorthoWebService");
答案 0 :(得分:1)
您缺少的部分是春季背景扫描。
来自Baeldung - A Guide to Apache CXF with Spring
首先,创建Spring应用程序上下文并将其配置为注册包含配置元数据的类:
AnnotationConfigWebApplicationContext context
= new AnnotationConfigWebApplicationContext();
context.register(ServiceConfiguration.class);
ServiceConfiguration类使用@Configuration批注进行批注,以提供bean定义。本课将在下一小节中讨论。 以下代码段显示了如何将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();
}
}