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 ip2region project, designed to simplify usage for Spring Boot applications.
Usage
2.1 Maven
<dependency>
<groupId>net.dreamlu</groupId>
<artifactId>mica-ip2region</artifactId>
<version>${version}</version>
</dependency>2.2 Gradle
compile("net.dreamlu:mica-ip2region:${version}")Configuration (built‑in, can be ignored)
Note: mica automatically synchronizes the ip2region.db file, so manual configuration is usually unnecessary.
Configuration key mica.ip2region.db-file-location defaults 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 .db file. Add the following plugin configuration to your pom.xml to prevent filtering:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>db</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>Documentation
4.1 Bean injection
@Autowired
private Ip2regionSearcher regionSearcher;4.2 Method reference
@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);4.3 Example service
public class Ip2regionServiceImpl implements IIp2regionService {
@Autowired
private Ip2regionSearcher regionSearcher;
@Override
public String getAddress(String ip) {
return regionSearcher.getAddress(ip);
}
}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).
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
