Get IP from specific URL in Spring Boot App

Viewed 28

I want to get IP address from domain name in Spring Boot App (not from request)

https://google.com ==> 142.251.40.142
1 Answers

At least you need to extract domain name or base url from the request. Then, use that domain name to get the IP Address via Java's InetAddress class.

Code :

@RequestMapping("/extract")
public void extract(HttpServletRequest request) throws MalformedURLException {
    String domainName = ServletUriComponentsBuilder.fromRequestUri(request)
        .replacePath(null)
        .build()
        .toUriString();
    InetAddress ip = InetAddress.getByName(new URL(domainName).getHost());
    System.out.println(ip);
}
Related