【问题标题】:Getting null from context从上下文中获取 null
【发布时间】:2019-09-02 09:07:55
【问题描述】:

为什么我在这个中将 resourceInfo 设为 null。有没有其他方法可以获取资源类和资源方法?

@Provider
@Authenticate
@Transactional
public class TestFilter2 implements ContainerRequestFilter {

  private final SessionRepository sessionRepository;

  @Context
  public ResourceInfo resourceInfo;

  @Inject
  public TestFilter2(SessionRepository sessionRepository) {
    this.sessionRepository = sessionRepository;
  }

  @Override
  public void filter(ContainerRequestContext requestContext) throws IOException {
    check();
  }


  private void check() {
    sessionRepository.getActiveSessionBySessionId("dsfsdf");

  }
}

【问题讨论】:

  • 尝试对两个依赖项使用相同类型的注入点,即两个字段或打扰构造函数。另外,您是通过扫描将过滤器注册为一个类,还是手动实例化它然后注册它?
  • 你解决了吗?

标签: java servlet-filters dropwizard


【解决方案1】:

@context注解不允许你注入ResourceInfo类型,here是你可以使用@context注入的类型:

  • 安全上下文
  • 请求
  • 资源上下文
  • Servlet 配置
  • ServletContext
  • HttpServletRequest
  • HttpServlet响应
  • HttpHeaders
  • UriInfo

【讨论】:

  • 即使这些也即将为空。
  • 你解决了吗?
猜你喜欢
  • 1970-01-01
  • 2011-11-09
  • 2014-03-26
  • 2011-07-15
  • 2016-11-24
  • 1970-01-01
  • 2014-04-12
  • 2018-11-02
  • 1970-01-01
相关资源
最近更新 更多