根据guice servlet文档 ,只要我设置guice过滤器并配置GuiceServletContextListener,我就应该能够在Web应用程序中使用Guice servlet。 事实是,我想对不带web.xml可嵌入tomcat使用完整的编程方法(例如,使用tomcat.addContext而不是tomcat.addWebapp ),并且在设置它时遇到了麻烦。

我的Main.java如下所示。

public class Main {
    private static String CONTEXT_PATH = "";
    public static void main(String[] args) throws LifecycleException {
        Tomcat tomcat = new Tomcat();
        String docBase = new File(".").getAbsolutePath();

        Context context = tomcat.addContext(CONTEXT_PATH, docBase);

        context.addApplicationListener(CustomGuiceServletContextListener.class.getName());

        context.addFilterDef(getGuiceFilterDef());
        context.addFilterMap(getGuiceFilterMap());

        tomcat.setPort(8080);
        tomcat.start();
        tomcat.getServer().await();
    }

    private static FilterMap getGuiceFilterMap() {
        FilterMap guiceFilterMap = new FilterMap();
        guiceFilterMap.setFilterName("guice");
        guiceFilterMap.addURLPattern("/*");
        return guiceFilterMap;
    }

    private static FilterDef getGuiceFilterDef() {
        FilterDef guiceFilterDef = new FilterDef();
        guiceFilterDef.setFilterName("guice");
        guiceFilterDef.setFilterClass("com.google.inject.servlet.GuiceFilter");
        guiceFilterDef.setFilter(new GuiceFilter());
        return guiceFilterDef;
    }
}

CustomGuiceServletContextListener.java

public class CustomGuiceServletContextListener extends GuiceServletContextListener {

    @Override
    protected Injector getInjector() {
        return Guice.createInjector(new ServletModule() {
            @Override
            protected void configureServlets() {
                serve("/*").with(PingServlet.class);
            }
        });
    }
}

PingServlet.java

@Singleton
public class PingServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().println("ping!");
        resp.getWriter().flush();
    }
}

如果运行该程序,tomcat服务器将成功启动,但是当我连接到服务器时,我的浏览器只会收到“ HTTP状态404 –找不到”错误。 是否有我必须添加到contexttomcat才能使其正常运行的东西,我缺少什么? 我是tomcat的新手,所以我可能不知道我必须做的某些设置。

下面是浏览器连接到Tomcat时生成的日志。 如您所见,tomcat似乎收到了请求,但无法将请求传递给我的自定义guice servlet上下文侦听器。

00:18:00.670 [http-nio-8080-exec-1] DEBUG - org.apache.juli.logging.DirectJDKLog - Received [GET / HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Cache-Control: max-age=0
DNT: 1
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36
Sec-Fetch-User: ?1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng;q=0.8,application/signed-exchange;v=b3
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9;q=0.8,es;q=0.7
Cookie: G_AUTHUSER_H=0

]
00:18:00.681 [http-nio-8080-exec-1] DEBUG - org.apache.juli.logging.DirectJDKLog - Set query string encoding to UTF-8
00:18:00.686 [http-nio-8080-exec-1] DEBUG - org.apache.juli.logging.DirectJDKLog - Cookies: Parsing b[]: G_AUTHUSER_H=0
00:18:00.688 [http-nio-8080-exec-1] DEBUG - org.apache.juli.logging.DirectJDKLog - Security checking request GET /
00:18:00.689 [http-nio-8080-exec-1] DEBUG - org.apache.juli.logging.DirectJDKLog -   No applicable constraints defined
00:18:00.700 [http-nio-8080-exec-1] DEBUG - org.apache.juli.logging.DirectJDKLog - Loading persistent provider registrations from [C:\Users\user\workspace\service\tomcat.8080\conf\jaspic-providers.xml]
00:18:00.701 [http-nio-8080-exec-1] DEBUG - org.apache.juli.logging.DirectJDKLog -  Not subject to any constraint
00:18:00.708 [http-nio-8080-exec-1] DEBUG - org.apache.juli.logging.DirectJDKLog - Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@7997a754:org.apache.tomcat.util.net.NioChannel@4d4fdca9:java.nio.channels.SocketChannel[connected local=/0:0:0:0:0:0:0:1:8080 remote=/0:0:0:0:0:0:0:1:4790]], Read from buffer: [0]
00:18:00.713 [http-nio-8080-exec-1] DEBUG - org.apache.juli.logging.DirectJDKLog - Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@7997a754:org.apache.tomcat.util.net.NioChannel@4d4fdca9:java.nio.channels.SocketChannel[connected local=/0:0:0:0:0:0:0:1:8080 remote=/0:0:0:0:0:0:0:1:4790]], Read direct from socket: [0]
00:18:00.714 [http-nio-8080-exec-1] DEBUG - org.apache.juli.logging.DirectJDKLog - Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@7997a754:org.apache.tomcat.util.net.NioChannel@4d4fdca9:java.nio.channels.SocketChannel[connected local=/0:0:0:0:0:0:0:1:8080 remote=/0:0:0:0:0:0:0:1:4790]], Status in: [OPEN_READ], State out: [OPEN]
00:18:00.714 [http-nio-8080-exec-1] DEBUG - org.apache.juli.logging.DirectJDKLog - Pushed Processor [org.apache.coyote.http11.Http11Processor@5a8cf651]
00:18:00.715 [http-nio-8080-exec-1] DEBUG - org.apache.juli.logging.DirectJDKLog - Registered read interest for [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@7997a754:org.apache.tomcat.util.net.NioChannel@4d4fdca9:java.nio.channels.SocketChannel[connected local=/0:0:0:0:0:0:0:1:8080 remote=/0:0:0:0:0:0:0:1:4790]]

如果这很重要,我正在使用tomcat-embed-core 8.5.49。

编辑

如果在Tomcat /上下文初始化期间在main方法中添加以下两行,则所有内容都会突然工作。 尽管下面的映射将给定的默认servlet(顺便说一句,它可以与任何存根servlet一起使用)指向/ ,但是该配置将被忽略,而我在GuiceServletContextListener中配置的内容似乎优先。

        // Default servlet
        Tomcat.addServlet(context, "default", "org.apache.catalina.servlets.DefaultServlet"); //any servlet works as a dummy placeholder
        context.addServletMappingDecoded("/", "default");

可嵌入的Tomcat是否需要至少添加1个映射才能开始提供请求? 以上两行的出现会产生差异,这似乎是异常的。 有没有一种方法可以解决问题,而不必添加上面无关的两行?

  ask by user482594 translate from so

本文未有回复,本站智能推荐: