简体   繁体   中英

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

There seem to be many questions arround it here but none helped me.... Tried to have single Java Class as startingpoint running embedded Jetty with Jersey to provide either Webpages and JSON interfaces...However even first step failed to provide multiple pages.

this works fine

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

but adding another content failed. How can I provide multiple pages providing different content types ? Is the only solution to added the content in that single EntryPoint class ?

Thanks in advance for any hint what is needed to change that

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

}

Actually found a solution. Missing Key Info was that you simple need for each and everything the right handler, put them in a handler list and voila there you are....

taken from the jetty documentation mostly after finding it

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

}

did help me...

I think that is this that you want:

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

You are not adding your ServletHolder instance to the ServletContextHandler .

Also both servlets have the same /* path, I'am not sure but this might not work, try attributing different paths and see if it works.

Do:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM