我已经尝试过here方法而没有运气:正如答案中的评论提到HttpServletRequest
只是null
。
这是我的过滤器:
public void myFilter(ContainerRequestContext request) throws IOException
{
// I don't see a way to get the IP address from the ContainerRequestContext
}
如果我尝试使用@Context HttpServletRequest httpServletRequest
,正如其他问题中的答案所示,我只得到NullPointerException
。
另一个类似的问题:How to get source address / ip from inside ContainerResponseFilter
答案 0 :(得分:0)
nullpointerexception仅在我们的测试中出现。 peeskillet帮助我意识到,只要我们使用HttpServletRequest
进行测试,注入jersey-test-framework-provider-jetty
就不会有效(请参阅例如https://github.com/jersey/jersey/issues/2764或{{ 3}})。如果我们使用GrizzlyWebTestContainerFactory
这样的内容,则注入HttpServletRequest
会有效,因为我们会有ServletDeploymentContext
。
我们在测试中选择了https://github.com/jersey/jersey/issues/3092,即只需要在需要的地方嘲笑HttpServletRequest
。