Search code examples
javajacksonmaxmindgeoip2

Java Geoip2: Getting "java.lang.reflect.InvocationTargetException" Exception


I'm using GeoIP2 in Java to convert IP address to location information from maxmind database.

My java code:

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);
}

I am getting java.lang.reflect.InvocationTargetException exception when I build my reader. I think jackson versions (2.5.3) are incompatible with geoip2. But I cannot fixed.

My dependecy versions are:

<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>   

Solution

  • Use 2.3.0 version of geoip2.

    <dependency>
        <groupId>com.maxmind.geoip2</groupId>
        <artifactId>geoip2</artifactId>
        <version>2.3.0</version>
    </dependency>
    

    2.8.1 uses newer version of Jackson, causing conflict with your Jackson dependency.