簡體   English   中英

帶有ServletServlet的嵌入式Jetty中的錯誤“多個servlet映射到路徑:/ *:”

[英]Error “Multiple servlets map to path: /*: ” in embedded Jetty with jerseyServlets

似乎這里有很多問題,但沒有一個對我有幫助。...試圖讓單個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();
    }
}

}

居然找到了解決方案。 缺少關鍵信息是,您簡單地需要正確的處理程序的每一個,並將它們放在處理程序列表中,瞧,到那里。

從碼頭文件中獲取,主要是在找到它之后

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

}

確實幫了我...

我認為這是您想要的:

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

您沒有將ServletHolder實例添加到ServletContextHandler

同樣,兩個servlet都具有相同的/*路徑,我不確定,但這可能行不通,請嘗試分配不同的路徑並查看其是否有效。

做:

context.addServlet(jerseyServlet,“ / jersey”);

context.addServlet(helloWorldServlet,“ / hello”);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM