如何在java中使用RESTful Web服务获取远程/客户端IP地址?

Ami*_*Ami 11 java rest web-services restful-authentication

我在我的项目中编写了Rest Web服务.Web服务调用可能来自不同的机器.所以我需要通过REST webservice找出IP地址.

从这个链接 request.getRemoteAddr()来使用它.

但我不能使用getRemoteAddr().因为我的请求和响应是xml格式.

我在REST服务中使用了post方法.Tomcat服务器.我有以xml格式发送请求.

我怎样才能获得IP地址?

Edw*_*rzo 34

假设您使用的是JAX-RS:

@GET
Produces("application/xlm")
public String getData(@Context HttpServletRequest request){
   String ip = request.getRemoteAddr();
}
Run Code Online (Sandbox Code Playgroud)

@Context注解允许你注入的情况下,

  • javax.ws.rs.core.HttpHeaders,
  • javax.ws.rs.core.UriInfo,
  • javax.ws.rs.core.Request,
  • javax.servlet.HttpServletRequest,
  • javax.servlet.HttpServletResponse,
  • javax.servlet.ServletConfig,
  • javax.servlet.ServletContext,和
  • javax.ws.rs.core.SecurityContext对象.