Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Remote / Client IP address using RESTful web service in java?

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.

From this link request.getRemoteAddr() to use this.

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

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

How can i get the IP address?

like image 363
Ami Avatar asked Jan 10 '13 09:01

Ami


People also ask

How do I find the IP address of a client connected to a server?

After the client establishes a successful connection to the server, the IP address of the client will be printed on the server console.

How do I find my REST API IP address?

getRemoteAddr() on the request object will give you the callers IP address.

How do you get the IP address of the client in servlet?

Methods to get client IP address in a servlet They are as follows: HttpServletRequest. getRemoteAddr(): This method returns the internet address of the client sending the request. HttpServletRequestgetRemoteHost(): This returns the host name of the client sending the request.


1 Answers

Assuming you are using JAX-RS:

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

The @Context annotation allows you to inject instances of

  • 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, and
  • javax.ws.rs.core.SecurityContext objects.
like image 86
Edwin Dalorzo Avatar answered Nov 09 '22 18:11

Edwin Dalorzo