2016-11-19 03:32:52 +05:30
|
|
|
package com.baeldung.geoip;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.net.InetAddress;
|
|
|
|
|
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
import com.maxmind.geoip2.DatabaseReader;
|
|
|
|
|
import com.maxmind.geoip2.exception.GeoIp2Exception;
|
|
|
|
|
import com.maxmind.geoip2.model.CityResponse;
|
|
|
|
|
|
|
|
|
|
public class GeoIpIntegrationTest {
|
2018-01-14 00:59:55 +02:00
|
|
|
|
2016-11-19 03:32:52 +05:30
|
|
|
@Test
|
|
|
|
|
public void givenIP_whenFetchingCity_thenReturnsCityData() throws IOException, GeoIp2Exception {
|
2016-11-23 09:41:02 +05:30
|
|
|
File database = new File("your-path-to-db-file");
|
2016-11-19 03:32:52 +05:30
|
|
|
DatabaseReader dbReader = new DatabaseReader.Builder(database).build();
|
2018-01-14 00:59:55 +02:00
|
|
|
|
2016-11-23 09:41:02 +05:30
|
|
|
InetAddress ipAddress = InetAddress.getByName("your-public-ip");
|
2016-11-19 03:32:52 +05:30
|
|
|
CityResponse response = dbReader.city(ipAddress);
|
2018-01-14 00:59:55 +02:00
|
|
|
|
2016-11-19 03:32:52 +05:30
|
|
|
String countryName = response.getCountry().getName();
|
|
|
|
|
String cityName = response.getCity().getName();
|
|
|
|
|
String postal = response.getPostal().getCode();
|
|
|
|
|
String state = response.getLeastSpecificSubdivision().getName();
|
2018-01-14 00:59:55 +02:00
|
|
|
|
2016-11-19 03:32:52 +05:30
|
|
|
}
|
2018-01-14 00:59:55 +02:00
|
|
|
|
2016-11-19 03:32:52 +05:30
|
|
|
}
|