HK2没有为球衣注入HttpServletRequest

HK2没有为球衣注入HttpServletRequest,第1张

HK2没有为球衣注入HttpServletRequest

您应该

@Override protected DeploymentContextconfigureDeployment()
JerseyTest
中返回
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));    }}
  • 您可以在源代码测试中看到更多示例


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5478660.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存