简体   繁体   English

带有ServletServlet的嵌入式Jetty中的错误“多个servlet映射到路径:/ *:”

[英]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. 似乎这里有很多问题,但没有一个对我有帮助。...试图让单个Java类作为起点运行带有Jersey的嵌入式Jetty来提供网页和JSON接口...但是,即使第一步也无法提供多个页面。

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 ? 是在单个EntryPoint类中添加内容的唯一解决方案吗?

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 . 您没有将ServletHolder实例添加到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. 同样,两个servlet都具有相同的/*路径,我不确定,但这可能行不通,请尝试分配不同的路径并查看其是否有效。

Do: 做:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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