maxmind / GeoIP2-java

Java API for GeoIP2 webservice client and database reader
https://maxmind.github.io/GeoIP2-java/
Apache License 2.0
783 stars 205 forks source link

ERROR: java.lang.ClassCastException: Cannot cast java.lang.String to com.maxmind.geoip2.model.AsnResponse. #270

Closed ScXin closed 3 years ago

ScXin commented 3 years ago

Hi,

I have used the com.maxmind.geoip2.geoip2-2.1.50 maven repo to match the asn info in Java. The used mmdb file is produced by mmdb writer(https://github.com/maxmind/mmdbwriter.git). However, this following error comes after the program run a range of time:

ERROR: java.lang.ClassCastException: Cannot cast java.lang.String to com.maxmind.geoip2.model.AsnResponse.

I don't know why this error happen. Who can help me?

oschwald commented 3 years ago

It sounds like you are inserting strings directly into the database. The MaxMind ASN database has a structure similar to the one in this example:

https://github.com/maxmind/mmdbwriter/blob/efe6d8ec58163fddff546376a8c2e8b9d0c38114/examples/asn-writer/main.go#L62-L70

If you want to decode a database where the data is just a string, you will need to used maxmind-db directly. I think something like this would work:

import com.maxmind.db.Reader;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;

public class Lookup {
    public static void main(String[] args) throws IOException {
        File database = new File("/path/to/database/custom-database.mmdb");
        try (Reader reader = new Reader(database)) {
            InetAddress address = InetAddress.getByName("24.24.24.24");

            String result = reader.get(address, String.class);

            System.out.println(result);
        }
    }
}
oschwald commented 3 years ago

Closing as I believe the above should solve this and the issue isn't directly related to this package.