相关疑难解决方法(0)

HK2 没有用球衣注入 HttpServletRequest

我试图按照此处的示例创建一个工厂,以便注入我的 HttpSession。不幸的是,无论我尝试什么,它都不起作用。不确定可能是什么问题。

我尝试仅注入 HttpServletRequest 和提供程序。这是我使用提供商的示例。该错误是尝试在提供方法中访问提供程序时出现空指针异常。如果我尝试注入 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) {
    }
}
Run Code Online (Sandbox Code Playgroud)

java dependency-injection jersey jersey-2.0 hk2

4
推荐指数
1
解决办法
3721
查看次数

Jersey JUnit 测试:@WebListener ServletContextListener 未调用

我在 Jersey (来自文档创建了这个测试,它工作正常,但有一个问题:@WebListener ServletContextListener没有被调用。

我需要测试的 Resource 类依赖于 ServletContextListener 在 ServletContext 上设置的属性。

我可以确保它被调用,或者我可以以其他方式操作 ServletContext 吗?

public class SimpleTest extends JerseyTest {

    @WebListener
    public static class AppContextListener implements ServletContextListener {

        @Override
        public void contextInitialized(ServletContextEvent event) {
            System.out.println("Context initialized");
        }

        @Override
        public void contextDestroyed(ServletContextEvent event) {
            System.out.println("Context destroyed");
        }
    }

    @Path("hello")
    public static class HelloResource {
        @GET
        public String getHello() {
            return "Hello World!";
        }
    }

    @Override
    protected Application configure() {
        return new ResourceConfig(HelloResource.class);
    }

    @Test
    public void test() …
Run Code Online (Sandbox Code Playgroud)

java rest junit jax-rs jersey

3
推荐指数
1
解决办法
2993
查看次数

标签 统计

java ×2

jersey ×2

dependency-injection ×1

hk2 ×1

jax-rs ×1

jersey-2.0 ×1

junit ×1

rest ×1