2

我正在使用 设置和应用程序,我已经为静态文件Undertow设置了一个,供apache-camel 用来公开其余服务。ResourceHandlerServlet

我已经在应用容器中使用 spring 和 servlet3.0 完成了这项工作。

在一个类扩展org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer

@Override
public void onStartup(ServletContext servletContext) throws ServletException
{
    super.onStartup(servletContext);
    ServletRegistration.Dynamic servlet = servletContext.addServlet("RestServlet", new CamelHttpTransportServlet());
    servlet.setLoadOnStartup(1);
    servlet.addMapping("/rest/*");
}

在骆驼路线

restConfiguration()
            .component("RestServlet")
            .bindingMode(RestBindingMode.json)
            .dataFormatProperty("prettyPrint", "true");

非常接近http://camel.apache.org/servlet.html中描述的内容

但是,如果我在 Undertow 中作为嵌入式执行此操作,org.apache.camel.NoSuchBeanException: No bean could be found in the registry for: RestServlet of type: org.apache.camel.spi.RestConsumerFactory我猜 Guice 永远不会找到由 Undertow 创建的 servlet。我试图将 CamelHttpTransportServlet 手动公开为 Guice 绑定,但这似乎并没有改变。

ClassLoader classLoader = getClass().getClassLoader();
    ResourceHandler staticHandler = new ResourceHandler(new ClassPathResourceManager(classLoader, STATIC_RESOURCE_ROOT))
            .addWelcomeFiles(INDEX_HTML);

    DeploymentInfo deploymentInfo = Servlets.deployment()
            .setClassLoader(classLoader)
            .setContextPath(ROOT_MAPPING)
            .setDeploymentName(DEPLOYMENT_NAME)
            .addServlet(
                    Servlets.servlet("RestServlet", CamelHttpTransportServlet.class)
                            .addMapping(REST_MAPPING)
                            .setLoadOnStartup(1)
            );

    DeploymentManager manager = Servlets.defaultContainer().addDeployment(deploymentInfo);
    manager.deploy();

    PathHandler path = Handlers.path()
            .addPrefixPath(REST_MAPPING, manager.start())
            .addPrefixPath(ROOT_MAPPING, staticHandler);

    undertow = Undertow.builder()
            .addHttpListener(port, LOCALHOST)
            .setHandler(path)
            .build();
    undertow.start();

静态资源按预期工作,似乎其余 servlet 正在运行并获得响应但CamelContext不会启动。

我不能在骆驼中使用restlet或任何东西,因为端口将被使用,所以我需要为静态文件和休息使用不同的端口。有没有办法让骆驼识别Servlet发起者Undertow

4

1 回答 1

1

好的,我终于发现了哪里出错了。

我怀疑我一直使用.component("servlet")and not .component("RestServlet"),但骆驼以前不会自动链接它。

我将此部分更改为

restConfiguration()
            .bindingMode(RestBindingMode.json)
            .component("servlet")
            .dataFormatProperty("prettyPrint", "true")
            .endpointProperty("servletName", "RestServlet);

并且我将 servlet 映射更改为的部署,/*否则request.getPathInfo()将返回null内部CamelHttpTransportServlet。注意我遇到了一个问题,因为我最初将 contextPath 设置为/rest/* 混乱的会话和 cookie

DeploymentInfo deploymentInfo = Servlets.deployment()
            .setClassLoader(classLoader)
            .setContextPath("/rest/")
            .setDeploymentName(DEPLOYMENT_NAME)
            .addServlet(
                    Servlets.servlet("RestServlet", CamelHttpTransportServlet.class)
                            .addMapping("/*")
                            .setLoadOnStartup(1)
            );
于 2015-10-04T14:55:25.003 回答