如何在Java中使用RESTful Web服务获取远程/客户端IP地址? [英] How to get Remote / Client IP address using RESTful web service in java?

查看:78
本文介绍了如何在Java中使用RESTful Web服务获取远程/客户端IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中编写了Rest Web服务.该Web服务调用可能来自其他计算机.因此,我需要通过REST Web服务查找IP地址.

I have written Rest web service in my project.The web service call may come from different machine.so i need to find out the IP address through REST webservice.

通过此链接 request.getRemoteAddr()来使用它.

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

But i cant use getRemoteAddr(). Because my request and response are xml format.

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

I have used post method in REST service.Tomcat server.I have send request as xml format.

我如何获取IP地址?

推荐答案

假设您正在使用JAX-RS:

Assuming you are using JAX-RS:

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

@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对象.

这篇关于如何在Java中使用RESTful Web服务获取远程/客户端IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆