java-HK2没有用球衣注入HttpServletRequest

我试图按照位于here的示例创建工厂以注入我的HttpSession.不幸的是,无论我尝试什么,都无法正常工作.不知道可能是什么问题.

我试过只注入HttpServletRequest和提供程序.这是我使用提供程序的示例.尝试在provide方法中访问提供程序时,该错误是空指针异常.如果我尝试注入HttpServletRequest,那么将无法获取任何对象.我正在使用JerseyTest在GrizzlyTestContainer中运行它.为了绑定HttpServletRequest,我需要添加一些内容吗?我似乎找不到一个例子.

public class HttpSessionFactory implements Factory<HttpSession> {

    private final HttpServletRequest request;

    @Inject
    public HttpSessionFactory(Provider<HttpServletRequest> requestProvider) {
        this.request = requestProvider.get();
    }

    @Override
    public HttpSession provide() {
       return request.getSession();
    }

    @Override
    public void dispose(HttpSession t) {
    }
}

解决方法:

您应该在JerseyTest中@Override受保护的DeploymentContext configureDeployment()返回ServletDeploymentContext.例如

import javax.inject.Inject;
import javax.inject.Provider;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import org.glassfish.hk2.api.Factory;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.servlet.ServletContainer;
import org.glassfish.jersey.test.DeploymentContext;
import org.glassfish.jersey.test.JerseyTest;
import org.glassfish.jersey.test.ServletDeploymentContext;
import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory;
import org.glassfish.jersey.test.spi.TestContainerException;
import org.glassfish.jersey.test.spi.TestContainerFactory;
import org.junit.Test;

public class ServletTest extends JerseyTest {

    @Path("/session")
    public static class SessionResource {

        @Inject
        HttpSession session;

        @GET
        public Response getSessionId() {
            return Response.ok(session.getId()).build();
        }
    }

    public static class HttpSessionFactory implements Factory<HttpSession> {

        private final HttpServletRequest request;

        @Inject
        public HttpSessionFactory(Provider<HttpServletRequest> requestProvider) {
            this.request = requestProvider.get();
        }

        @Override
        public HttpSession provide() {
            return request.getSession();
        }

        @Override
        public void dispose(HttpSession t) {
        }
    }

    @Override
    protected TestContainerFactory getTestContainerFactory() {
        return new GrizzlyWebTestContainerFactory();
    }

    @Override
    protected DeploymentContext configureDeployment() {
        ResourceConfig config = new ResourceConfig(SessionResource.class);
        config.register(new AbstractBinder() {
            @Override
            protected void configure() {
                bindFactory(HttpSessionFactory.class).to(HttpSession.class);
            }
        });
        return ServletDeploymentContext.forServlet(
                                 new ServletContainer(config)).build();
    }

    @Test
    public void test() {
        System.out.println(target("session").request().get(String.class));
    }
}

>您可以在source code tests中看到更多示例

上一篇:1. Netty准备知识:Java NIO


下一篇:java-XMLRootElement在jersey中将类转换为XML