【问题标题】:Error "Multiple servlets map to path: /*: " in embedded Jetty with jerseyServlets带有 jerseyServlets 的嵌入式 Jetty 中出现错误“多个 servlet 映射到路径:/*:”
【发布时间】:2015-08-10 15:43:15
【问题描述】:

这里似乎有很多问题,但没有一个对我有帮助.... 尝试将单个 Java 类作为起点,使用 Jersey 运行嵌入式 Jetty,以提供网页和 JSON 接口......但是,即使第一步也未能提供多个页面。

这很好用

ServletHolder jerseyServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
jerseyServlet.setInitOrder(0);      
jerseyServlet.setInitParameter("jersey.config.server.provider.classnames", EntryPoint.class.getCanonicalName());

但添加其他内容失败。如何提供提供不同内容类型的多个页面? 是在单个 EntryPoint 类中添加内容的唯一解决方案吗?

提前感谢任何提示需要改变它

public class App {
public static void main(String[] args) throws Exception {
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    Server jettyServer = new Server(8080);
    jettyServer.setHandler(context);

    ServletHolder jerseyServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
    jerseyServlet.setInitOrder(0);                                                                                  
    jerseyServlet.setInitParameter("jersey.config.server.provider.classnames", EntryPoint.class.getCanonicalName());

    ServletHolder helloWorldServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
    helloWorldServlet.setInitOrder(1);                                                                                  
    helloWorldServlet.setInitParameter("jersey.config.server.provider.classnames", HelloWorldService.class.getCanonicalName());

    try {
        jettyServer.start();
        jettyServer.join();
    } catch (Exception e){
        System.out.println("Failed running jettyServer with " + e.getMessage());
    } finally {
        jettyServer.destroy();
    }
}

}

【问题讨论】:

  • 您创建了一个 ServletHolder,但您是否将其添加到 ServletContextHandler,即context.addServlet(jerseyServlet)?如果你能提供更多的代码,人们会有更好的参考来帮助你。
  • 尝试了很多。希望完整尝试的添加代码可以解释它
  • 我想我必须放弃并通过一个入口点球衣提供商做所有事情
  • 使用ServerProperties.PROVIDER_CLASSNAMES 而不是jersey.config.server.provider.classnames

标签: java jersey embedded-jetty


【解决方案1】:

其实找到了解决办法。 缺少关键信息是您简单地需要正确的处理程序,将它们放在处理程序列表中,瞧,您就在那里......

大部分是在找到它后取自码头文档

public class JettyServer
{
public static void main(String[] args) throws Exception
{
    // Create a basic Jetty server object that will listen on port 8080.  Note that if you set this to port 0
    // then a randomly available port will be assigned that you can either look in the logs for the port,
    // or programmatically obtain it for use in test cases.
    Server server = new Server(8080);

    // Create the ResourceHandler. It is the object that will actually handle the request for a given file. It is
    // a Jetty Handler object so it is suitable for chaining with other handlers as you will see in other examples.
    ResourceHandler resource_handler = new ResourceHandler();
    // Configure the ResourceHandler. Setting the resource base indicates where the files should be served out of.
    // In this example it is the current directory but it can be configured to anything that the jvm has access to.
    resource_handler.setDirectoriesListed(true);
    resource_handler.setWelcomeFiles(new String[]{ "./html/index.html" });
    resource_handler.setResourceBase(".");

    //Jersey ServletContextHandler
    ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
    ServletHolder jerseyServlet = servletContextHandler.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/api/*");
    jerseyServlet.setInitOrder(0);                                                                                  
    jerseyServlet.setInitParameter("jersey.config.server.provider.classnames", EntryPoint.class.getCanonicalName());

    // Add the ResourceHandler to the server.
    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { resource_handler, servletContextHandler, new DefaultHandler() });
    server.setHandler(handlers);

    // Start things up! By using the server.join() the server thread will join with the current thread.
    // See "http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join()" for more details.
    server.start();
    server.join();
}

}

帮了我...

【讨论】:

  • 使用ServerProperties.PROVIDER_CLASSNAMES 而不是jersey.config.server.provider.classnames
  • 谢谢,但正如去年提到的,该解决方案确实对我有用
【解决方案2】:

我想这就是你想要的:

jerseyServlet.setInitParameter("jersey.config.server.provider.classnames",
             String.join(",", Arrays.asList(EntryPoint.class.getCanonicalName(),
                     HelloWorldService.class.getCanonicalName())));

【讨论】:

    【解决方案3】:

    您没有将ServletHolder 实例添加到ServletContextHandler

    另外两个 servlet 具有相同的 /* 路径,我不确定,但这可能不起作用,尝试归因于不同的路径,看看它是否有效。

    做:

    context.addServlet(jerseyServlet, "/jersey");

    context.addServlet(helloWorldServlet , "/hello");

    【讨论】:

    • 谢谢,但如果我删除 helloWorldServlet,它会在初始化时正常工作。您可能是对的,我必须更改它以添加多个 Servlet。仍然不清楚比球衣课程看起来如何。 Stillnopt 在网络中查找任何有效的示例
    • 您可以使用此文档作为参考 create servlets 并设置 Servlet Context。至于您在标题中提出的问题,问题是您有两个 servlet 映射到与我在回答中输入的路径相同的路径。但似乎这不是唯一的问题。
    • 谢谢,我做到了,但我也是泽西岛 (JAX-RS) 的新手,所有示例都依赖于带有 iniProcess 的 ServletHolder。我仍然不清楚这一切是如何相互作用的。您的链接参考中没有提到初始化...无论如何,如上所述,我放弃并继续使用一个 entryPoint 类
    猜你喜欢
    • 2021-07-03
    • 1970-01-01
    • 2015-10-01
    • 1970-01-01
    • 2015-02-25
    • 2012-12-06
    • 2016-10-14
    • 2019-05-14
    • 2012-11-23
    相关资源
    最近更新 更多