如何在Jersey 2.25.1应用程序中获取调用者的IP地址?

时间:2018-01-23 10:19:26

标签: jersey-2.0 jetty-9

我已经尝试过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

1 个答案:

答案 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