JERSEY: How to retrieve the calling IP or URI using injection annotation?

后端 未结 3 1795
生来不讨喜
生来不讨喜 2021-02-13 10:28

I have a REST- Server here using JERSEY. I must log the IP (better the DNS) of the calling client.

Can somebody point a direction which injection annotations to use ?

3条回答
  •  一向
    一向 (楼主)
    2021-02-13 11:06

    Option 2 for Grizzly-Jersey combo. Place in the declaration of the class (extender of ContainerRequestFilter in my case)

     @Inject
     private javax.inject.Provider request;
    

    and later in the code use this.

    request.get().getRemoteAddr()
    

    I dug around and I've found the resolution in the jersey's jira. Note that they recommend to be used @Inject instead of @Context

    I've tried to use

      @Context
      private HttpServletRequest servletRequest;
    

    which is recommended widely but servletRequest was always null.

    *comment servletRequest was null because I was used GrizzlyHttpServerFactory for creating HttpServer. If you want to have servletRequest then you need to deploy it with WebappContext. Take a look here for details

提交回复
热议问题