我在 Java 中使用 GeoIP2 将 IP 地址转换为来自 maxmind 数据库的位置信息。
我的java代码:
try {
ClassLoader classLoader = getClass().getClassLoader();
File database = new File(classLoader.getResource("GeoLite2-City.mmdb").getFile());
DatabaseReader reader = new DatabaseReader.Builder(database).build();
InetAddress ipAddress = InetAddress.getByName(ip);
CityResponse response = reader.city(ipAddress);
City city = response.getCity();
...
} catch (UnknownHostException e) {
logger.logError(getClass(), "getGeoIp", " transactionId:" + transactionId + " > " + e.getMessage(), null);
} catch (IOException e) {
logger.logError(getClass(), "getGeoIp", " transactionId:" + transactionId + " > " + e.getMessage(), null);
} catch (GeoIp2Exception e) {
logger.logError(getClass(), "getGeoIp", " transactionId:" + transactionId + " > " + e.getMessage(), null);
}
当我构建我的阅读器时,我遇到了java.lang.reflect.InvocationTargetException
异常。我认为杰克逊版本(2.5.3)与geoip2不兼容。但我无法修复。
我的依赖版本是:
<dependency>
<groupId>com.maxmind.geoip2</groupId>
<artifactId>geoip2</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>