我有一个包含 2 个模块的项目:Rest 和 Service。我需要服务模块中的客户端 IP,但我只能从请求的 Rest 模块中获取它。我考虑使用 EJBContext.getContextData() 在其他模块中提供此 IP。
在休息时我创建:
@Provider
@ApplicationScoped
public class MyContainerRequestFilter implements ContainerRequestFilter {
@Context
private HttpServletRequest servletRequest;
@Inject
private ApplicationService applicationService;
@Override
public void filter(ContainerRequestContext requestContext) {
try {
applicationService.getEJBContext().getContextData().put("ipAddress", servletRequest.getRemoteAddr());
applicationService.getEJBContext().getContextData().put("ipHost", servletRequest.getRemoteHost());
} catch(Exception ingnore) {
}
}
}
@LocalBean
@Stateless
public class ApplicationService {
@Resource
private EJBContext ejbContext;
public EJBContext getEJBContext() {
return ejbContext;
}
}
如果我使用
@Resource 私有 EJBContext ejbContext;
在 MyContainerRequestFilter - 然后 ejbContext = null;
所以,在我的例子中
applicationService.getEJBContext().getContextData() - NullPointerException
告诉我出了什么事?知道我该怎么做吗?
谢谢!