To improve performance, reuse // the object across lookups. A File object pointing to your GeoIP2 Enterprise database File database = new File ( "/path/to/GeoIP2-Enterprise.mmdb" ) // This creates the DatabaseReader object. getLongitude ()) // -93.2323 Anonymous IP getCode ()) // '55455' Location location = response. getName ()) // 'Minneapolis' Postal postal = response. getIsoCode ()) // 'MN' City city = response. get ( "zh-CN" )) // '美国' Subdivision subdivision = response. city ( ipAddress ) Country country = response. getByName ( "128.101.101.101" ) // Replace "city" with the appropriate method for your database, e.g., // "country". build () InetAddress ipAddress = InetAddress. DatabaseReader reader = new DatabaseReader. A File object pointing to your GeoIP2 or GeoLite2 database File database = new File ( "/path/to/GeoIP2-City.mmdb" ) // This creates the DatabaseReader object. Using this cache, lookup performance is significantly improved at the cost of A simple implementation is provided by .CHMCache. The database API supports pluggable caching (by default, no caching is See the API documentation for more details. The creation of this object is relatively expensive as it We recommend reusing the DatabaseReader object rather than creating a new Which represents part of the data returned by the database. The class in turn contains multiple record classes, each of If the lookup succeeds, the method call will return a response class for the Method will be slightly faster as they do not need to construct and throwĪn exception. If youĪre looking up many IPs that are not contained in the database, the try
The method without the prefix will throw anĪddressNotFoundException if the address is not in the database. Prefix return an Optional object, which will be empty if the value is Methods take the IP address to be looked up. You may also specify theįileMode and the locales fallback order using the methods on the BuilderĪfter you have created the DatabaseReader, you may then call one of theĪppropriate methods, e.g., city or tr圜ity, for your database. InputStream or File for your GeoIP2 database. You must provide the Builder constructor either an To use the database API, you must create a new DatabaseReader using theĭatabaseReader.Builder.
new WebServiceClient.Builder(42, "license_key").host("").build() try ( WebServiceClient client = new WebServiceClient. To use the GeoLite2 web service instead of GeoIP2 Precision, call the // host method on the builder with "", e.g.
// Replace "42" with your account ID and "license_key" with your license key. The object is closeable, but // it should not be closed until you are finished making requests with it. Reusing the object will allow it to keep // connections alive for future requests. This creates a WebServiceClient object that is thread-safe and can be // reused across requests. Resources are promptly returned to the system. Should close the object to ensure the connections are closed and any Once you have finished making requests, you Making multiple requests, the object should be reused so that new connectionsĪre not created for each request. The WebServiceClient object is safe to share across threads. If the request fails, the client class throws an exception.
Which represents part of the data returned by the web service. This model in turn contains multiple record classes, each of If the request succeeds, the method call will return a model class for the end The method corresponding to a specific end point, passing it the IP address After you have created the WebServiceClient, you may then call Set a timeout or set the locales fallback order using the methods on theīuilder. Of GeoIP2, set the host method on the builder to. You must provide the Builder constructor your To use the web service API, you must create a new WebServiceClient using the Should not be used to identify a particular address or household. Any location provided by a GeoIP2 database or web service If you are unable to use Maven or Gradle, you may include the geoip2.jarįile and its dependencies in your classpath.