@@ -10,7 +10,7 @@ format that stores data indexed by IP address subnets (IPv4 or IPv6).
1010
1111### Maven ###
1212
13- We recommend installing this package with [ Maven] ( http ://maven.apache.org/) .
13+ We recommend installing this package with [ Maven] ( https ://maven.apache.org/) .
1414To do this, add the dependency to your pom.xml:
1515
1616``` xml
@@ -37,7 +37,7 @@ dependencies {
3737## Usage ##
3838
3939* Note:* For accessing MaxMind GeoIP2 databases, we generally recommend using
40- the [ GeoIP2 Java API] ( http ://maxmind.github.io/GeoIP2-java/) rather than using
40+ the [ GeoIP2 Java API] ( https ://maxmind.github.io/GeoIP2-java/) rather than using
4141this package directly.
4242
4343To use the API, you must first create a ` Reader ` object. The constructor for
@@ -48,11 +48,9 @@ memory. This often provides performance comparable to loading the file into
4848real memory with ` MEMORY ` .
4949
5050To look up an IP address, pass the address as an ` InetAddress ` to the ` get `
51- method on ` Reader ` . This method will return the result as a
52- ` com.fasterxml.jackson.databind.JsonNode ` object. ` JsonNode ` objects are used
53- as they provide a convenient representation of multi-type data structures and
54- the databind package of Jackson 2 supplies many tools for interacting with the
55- data in this format.
51+ method on ` Reader ` , along with the class of the object you want to
52+ deserialize into. This method will create an instance of the class and
53+ populate it. See examples below.
5654
5755We recommend reusing the ` Reader ` object rather than creating a new one for
5856each lookup. The creation of this object is relatively expensive as it must
@@ -61,7 +59,8 @@ read in metadata for the file.
6159## Example ##
6260
6361``` java
64- import com.fasterxml.jackson.databind.JsonNode ;
62+ import com.maxmind.db.MaxMindDbConstructor ;
63+ import com.maxmind.db.MaxMindDbParameter ;
6564import com.maxmind.db.Reader ;
6665import com.maxmind.db.Record ;
6766
@@ -73,26 +72,56 @@ public class Lookup {
7372 public static void main (String [] args ) throws IOException {
7473 File database = new File (" /path/to/database/GeoIP2-City.mmdb" );
7574 try (Reader reader = new Reader (database)) {
76-
7775 InetAddress address = InetAddress . getByName(" 24.24.24.24" );
7876
7977 // get() returns just the data for the associated record
80- JsonNode recordData = reader. get(address);
78+ LookupResult result = reader. get(address, LookupResult . class );
8179
82- System . out. println(recordData );
80+ System . out. println(result . getCountry() . getIsoCode() );
8381
8482 // getRecord() returns a Record class that contains both
8583 // the data for the record and associated metadata.
86- Record record = reader. getRecord(address);
84+ Record<LookupResult > record
85+ = reader. getRecord(address, LookupResult . class);
8786
88- System . out. println(record. getData());
87+ System . out. println(record. getData(). getCountry() . getIsoCode() );
8988 System . out. println(record. getNetwork());
9089 }
9190 }
91+
92+ public static class LookupResult {
93+ private final Country country;
94+
95+ @MaxMindDbConstructor
96+ public LookupResult (
97+ @MaxMindDbParameter (name = " country" ) Country country
98+ ) {
99+ this . country = country;
100+ }
101+
102+ public Country getCountry () {
103+ return this . country;
104+ }
105+ }
106+
107+ public static class Country {
108+ private final String isoCode;
109+
110+ @MaxMindDbConstructor
111+ public Country (
112+ @MaxMindDbParameter (name = " iso_code" ) String isoCode
113+ ) {
114+ this . isoCode = isoCode;
115+ }
116+
117+ public String getIsoCode () {
118+ return this . isoCode;
119+ }
120+ }
92121}
93122```
94123
95- ### Caching # ##
124+ ## Caching ##
96125
97126The database API supports pluggable caching (by default, no caching is
98127performed). A simple implementation is provided by ` com.maxmind.db.CHMCache ` .
@@ -105,6 +134,10 @@ Usage:
105134Reader reader = new Reader (database, new CHMCache ());
106135```
107136
137+ Please note that the cache will hold references to the objects created
138+ during the lookup. If you mutate the objects, the mutated objects will be
139+ returned from the cache on subsequent lookups.
140+
108141## Multi-Threaded Use ##
109142
110143This API fully supports use in multi-threaded applications. In such
@@ -130,7 +163,7 @@ version. You may also call `System.gc()` after dereferencing the
130163
131164If you are packaging the database file as a resource in a JAR file using
132165Maven, you must
133- [ disable binary file filtering] ( http ://maven.apache.org/plugins/maven-resources-plugin/examples/binaries-filtering.html) .
166+ [ disable binary file filtering] ( https ://maven.apache.org/plugins/maven-resources-plugin/examples/binaries-filtering.html) .
134167Failure to do so will result in ` InvalidDatabaseException ` exceptions being
135168thrown when querying the database.
136169
@@ -139,18 +172,18 @@ thrown when querying the database.
139172The MaxMind DB format is an open format for quickly mapping IP addresses to
140173records. The
141174[ specification] ( https://github.com/maxmind/MaxMind-DB/blob/master/MaxMind-DB-spec.md )
142- is available as part of our
175+ is available, as is our
143176[ Perl writer] ( https://github.com/maxmind/MaxMind-DB-Writer-perl ) for the
144177format.
145178
146179## Bug Tracker ##
147180
148- Please report all issues with this code using the [ GitHub issue tracker ]
149- (https://github.com/maxmind/MaxMind-DB-Reader-java/issues ).
181+ Please report all issues with this code using the [ GitHub issue
182+ tracker ] ( https://github.com/maxmind/MaxMind-DB-Reader-java/issues ) .
150183
151184If you are having an issue with a MaxMind database or service that is not
152185specific to this reader, please [ contact MaxMind support]
153- (http ://www.maxmind.com/en/support ).
186+ (https ://www.maxmind.com/en/support ).
154187
155188## Requirements ##
156189
@@ -163,7 +196,7 @@ possible.
163196
164197## Versioning ##
165198
166- The MaxMind DB Reader API uses [ Semantic Versioning] ( http ://semver.org/) .
199+ The MaxMind DB Reader API uses [ Semantic Versioning] ( https ://semver.org/) .
167200
168201## Copyright and License ##
169202
0 commit comments