Hey guys I have a little issue here, in my application I manage to get users ip thanks to this implementation how to mark user's current location on Primefaces Gmap? its works fine heres how I implement it:
FacesContext context = FacesContext.getCurrentInstance();
ExternalContext externalContext = context.getExternalContext();
HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
String ip = request.getRemoteAddr();
System.out.println("ip: " + ip);
The issue is that now I need to do the same but in a rest service, but in a rest service I can't get the FacesContext
so I can't get the ExternalContext
and so on... do you know hows the way to achieve the same via rest service? I tried this:
Accessing FacesContext from Web Service
but no luck, this is what I have at the moment, im testing the service with POSTMAN:
@POST
@Path("/test")
public void test() {
ServletContext servletContext = (ServletContext) context;
HttpServletRequest request = (HttpServletRequest) servletContext;
String ip = request.getRemoteAddr();
System.out.println("ip: " + ip);
}
This is how I declare context variable:
@Resource
WebServiceContext context;
This second code doesn't pass the HttpServletRequest request = (HttpServletRequest) servletContext;
it gives me the error
Caused by: java.lang.ClassCastException: org.jboss.ws.common.injection.ThreadLocalAwareWebServiceContext cannot be cast to javax.servlet.ServletContext