简体   繁体   中英

Servlet with Guice and Jetty Embedded

I am trying to use Guice Servlet with Jetty to inject some object inside a servlet.

I have created the servlet in this way:

injector = injector.createChildInjector( new ServletModule() 
        {
            @Override
            protected void configureServlets() {
            serve("/jsonServlet").with(JsonServlet.class);
            }
        }
    );

logger.info("Start Hermes server");
JettyServer jettyServer = injector.getInstance(JettyServer.class);
jettyServer.start();

The Jetty server is started in this way:

QueuedThreadPool threadPool = new QueuedThreadPool(MAX_THREADS, MIN_THREADS, IDLE_TIMEOUT);
Server server = new Server(threadPool);
try (ServerConnector connector = new ServerConnector(server)) {
    connector.setPort(serverBindPort);
    connector.setHost(serverBindAddress.getHostAddress());
    server.setConnectors(new Connector[] { connector });

    ServletHandler servletHandler = new ServletHandler();
    servletHandler.addFilterWithMapping(com.google.inject.servlet.GuiceFilter.class, "/*", 1);
    
    server.setHandler(servletHandler);

    server.start();
    server.join();
}

And the jsonServlet is this one:

public JsonServlet(){
    logger.info("Called no arguments constructor");
}

@Inject
public JsonServlet(@DatabaseUri String databaseUri,
                    @DatabaseUserName String databaseUserName,
                    @DatabasePassword String databasePassword){
    logger.info("Called Guice constructor");
    this.databaseUri = databaseUri;
    this.databaseUserName = databaseUserName;
    this.databasePassword = databasePassword;
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ 
    logger.info("I am inside the doPost");
    logger.info("Data base Uri is = {}", databaseUri);
}

The App start and jetty wait for the first post, unfortunately as soon as the post arrive a Null pointer exception has fired:

[INFO ] 23:31:01.670 it.fox.hermes.servlets.JsonServlet.<init>() - Called Guice constructor
[INFO ] 23:31:01.671 it.fox.hermes.servlets.JsonServlet.init() - Init Receive Json servlet
[INFO ] 23:31:01.701 org.eclipse.jetty.server.AbstractConnector.doStart() - Started ServerConnector@3c989952{HTTP/1.1, (http/1.1)}{0.0.0.0:8090}
[INFO ] 23:31:01.701 org.eclipse.jetty.server.Server.doStart() - Started @1026ms
[WARN ] 23:31:09.044 org.eclipse.jetty.server.HttpChannel.handleException() - /receiveJson
java.lang.NullPointerException: null
        at com.google.inject.servlet.ServletUtils.getContextRelativePath(ServletUtils.java:57) ~[guice-servlet-4.2.3.jar:?]
        at com.google.inject.servlet.ServletDefinition.service(ServletDefinition.java:178) ~[guice-servlet-4.2.3.jar:?]
        at com.google.inject.servlet.ManagedServletPipeline.service(ManagedServletPipeline.java:89) ~[guice-servlet-4.2.3.jar:?]
        at com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:121) ~[guice-servlet-4.2.3.jar:?]
        at com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:133) ~[guice-servlet-4.2.3.jar:?]
        at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1638) ~[jetty-servlet-9.4.31.v20200723.jar:9.4.31.v20200723]
        at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:567) ~[jetty-servlet-9.4.31.v20200723.jar:9.4.31.v20200723]
        at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:190) ~[jetty-server-9.4.31.v20200723.jar:9.4.31.v20200723]
        at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:507) ~[jetty-servlet-9.4.31.v20200723.jar:9.4.31.v20200723]
        at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) ~[jetty-server-9.4.31.v20200723.jar:9.4.31.v20200723]
        at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127) ~[jetty-server-9.4.31.v20200723.jar:9.4.31.v20200723]
        at org.eclipse.jetty.server.Server.handle(Server.java:501) ~[jetty-server-9.4.31.v20200723.jar:9.4.31.v20200723]
        at org.eclipse.jetty.server.HttpChannel.lambda$handle$1(HttpChannel.java:383) ~[jetty-server-9.4.31.v20200723.jar:9.4.31.v20200723]
        at org.eclipse.jetty.server.HttpChannel.dispatch(HttpChannel.java:556) ~[jetty-server-9.4.31.v20200723.jar:9.4.31.v20200723]
        at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:375) [jetty-server-9.4.31.v20200723.jar:9.4.31.v20200723]
        at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:273) [jetty-server-9.4.31.v20200723.jar:9.4.31.v20200723]
        at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:311) [jetty-io-9.4.31.v20200723.jar:9.4.31.v20200723]
        at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:105) [jetty-io-9.4.31.v20200723.jar:9.4.31.v20200723]
        at org.eclipse.jetty.io.ChannelEndPoint$1.run(ChannelEndPoint.java:104) [jetty-io-9.4.31.v20200723.jar:9.4.31.v20200723]
        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:806) [jetty-util-9.4.31.v20200723.jar:9.4.31.v20200723]
        at org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run(QueuedThreadPool.java:938) [jetty-util-9.4.31.v20200723.jar:9.4.31.v20200723]
        at java.lang.Thread.run(Thread.java:830) [?:?]

I can't understand what this null pointer came from and where is the error.

Thanks, Stefano

Probably something is wrong in the using of filter required by Guice.

Here is the way I found to solve the problem:

QueuedThreadPool threadPool = new QueuedThreadPool(MAX_THREADS, MIN_THREADS, IDLE_TIMEOUT);

server = new Server(threadPool);
try (ServerConnector connector = new ServerConnector(server)) {
    connector.setPort(serverBindPort);
    connector.setHost(serverBindAddress.getHostAddress());
    server.setConnectors(new Connector[] { connector });
    // Guice Injection in servlets
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    ServletHandler handler = new ServletHandler();
    FilterHolder fh = handler.addFilterWithMapping(com.google.inject.servlet.GuiceFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
    context.addFilter(fh, "/*", EnumSet.of(DispatcherType.REQUEST));
    server.setHandler(context);

    server.start();
    server.join();

}

In this way injection in a servlet with jetty embedded works.

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