简体   繁体   中英

How can I get the location(city,country.. ) from HttpServletRequest using spring boot?

I want to know the location from httpServletRequest. Is there any way to do that? I want to use one of the getters methods of httpServletRequest like getRemoteAddr().. Thanks in advance.

This looks old post. For upcoming visitors adding this answer.

Try to use httpServletRequest.getLocale().getCountry() this will return country name as string.

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)

RequestContext requestContext = new RequestContext(httpServletRequest);
String countryISO3 = requestContext.getLocale().getISO3Country();

It has many methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM