Integrate ip2region with Spring Boot Using the mica-ip2region Starter
This guide explains how to add the mica-ip2region starter to a Spring Boot project, covering Maven and Gradle dependencies, built‑in configuration, custom Maven settings for the ip2region.db file, bean injection, available lookup methods, and a complete service example.
Introduction
mica-ip2regionis a starter wrapper for the open‑source
ip2regionproject, designed to simplify usage for Spring Boot applications.
Usage
2.1 Maven
<code><dependency>
<groupId>net.dreamlu</groupId>
<artifactId>mica-ip2region</artifactId>
<version>${version}</version>
</dependency></code>2.2 Gradle
<code>compile("net.dreamlu:mica-ip2region:${version}")</code>Configuration (built‑in, can be ignored)
Note: mica automatically synchronizes the
ip2region.dbfile, so manual configuration is usually unnecessary.
Configuration key
mica.ip2region.db-file-locationdefaults to
classpath:ip2region/ip2region.db, which points to the bundled database file.
Maven custom ip2region.db handling
When Maven copies resources, it applies a filter that can corrupt the binary
.dbfile. Add the following plugin configuration to your
pom.xmlto prevent filtering:
<code><plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>db</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin></code>Documentation
4.1 Bean injection
<code>@Autowired
private Ip2regionSearcher regionSearcher;</code>4.2 Method reference
<code>@Nullable
IpInfo memorySearch(long ip);
@Nullable
IpInfo memorySearch(String ip);
@Nullable
IpInfo getByIndexPtr(long ptr);
@Nullable
IpInfo binarySearch(long ip);
@Nullable
IpInfo binarySearch(String ip);
@Nullable
String getAddress(long ip);
@Nullable
String getAddress(String ip);
@Nullable
String getAddressAndIsp(long ip);
@Nullable
String getAddressAndIsp(String ip);
</code>4.3 Example service
<code>public class Ip2regionServiceImpl implements IIp2regionService {
@Autowired
private Ip2regionSearcher regionSearcher;
@Override
public String getAddress(String ip) {
return regionSearcher.getAddress(ip);
}
}
</code>About the mica component suite
The author maintains several open‑source projects under the mica suite, including mica-auto (Spring Boot starter utilities), mica (microservice component collection), and mica-mqtt (IoT MQTT component based on t‑io).
Java Architecture Diary
Committed to sharing original, high‑quality technical articles; no fluff or promotional content.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.