[英]How can I get the location(city,country.. ) from HttpServletRequest using spring boot?
I want to know the location from httpServletRequest.我想知道来自 httpServletRequest 的位置。 Is there any way to do that?
有没有办法做到这一点? I want to use one of the getters methods of httpServletRequest like getRemoteAddr().. Thanks in advance.
我想使用 httpServletRequest 的 getter 方法之一,例如 getRemoteAddr().. 在此先感谢。
This looks old post.这看起来很旧。 For upcoming visitors adding this answer.
对于即将到来的访客添加此答案。
Try to use httpServletRequest.getLocale().getCountry()
this will return country name as string.尝试使用
httpServletRequest.getLocale().getCountry()
这将返回国家名称作为字符串。
Another option to get more precise values like ISO3 country code, ISO language codes use below methods Pass the HttpServletRequest object to RequestContext(org.springframework.web.servlet.support.RequestContext)获得更精确值的另一种选择,如 ISO3 国家代码、ISO 语言代码使用以下方法将 HttpServletRequest object 传递给 RequestContext(org.springframework.web.servlet.support.RequestContext)
RequestContext requestContext = new RequestContext(httpServletRequest);
String countryISO3 = requestContext.getLocale().getISO3Country();
It has many methods.它有很多方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.