Backend Development 5 min read

How to Use ip2region in Spring Boot for Java IP Geolocation

This guide introduces ip2region, an offline IP geolocation library, and shows how to integrate its Java wrapper mica‑ip2region into Spring Boot, covering dependency setup, bean injection, search methods, IpInfo fields, and provides demo screenshots and related open‑source links.

Java Architecture Diary
Java Architecture Diary
Java Architecture Diary
How to Use ip2region in Spring Boot for Java IP Geolocation

1. Introduction

Recently many platforms have launched IP address display features (e.g., Toutiao, Baidu, Weibo, Xiaohongshu, Zhihu, and possibly WeChat public accounts) to help prevent online fraud, rumors, attacks, and impersonation, aiming to create a safer, greener internet. This article explains how to use Java to obtain IP address location.

2. What is ip2region?

ip2region is an offline IP address location library with 99.9% accuracy, millisecond‑level query speed, a database of only a few megabytes, and provides Java, PHP, C, Python, Node.js, Go, C# bindings as well as Binary, B‑tree, and memory search algorithms.

3. mica-ip2region

mica-ip2region

is a wrapper around

ip2region

that simplifies usage for Spring Boot users.

3.1 Add Dependency

<code>
<dependency>
  <groupId>net.dreamlu</groupId>
  <artifactId>mica-ip2region</artifactId>
  <version>${version}</version>
</dependency>
</code>

3.2 Usage Documentation

Inject Bean

<code>
@Autowired
private Ip2regionSearcher regionSearcher;
</code>

Method Description

<code>
/**
 * IP location search
 *
 * @param ip ip
 * @return location
 */
@Nullable
IpInfo memorySearch(long ip);

/**
 * IP location search
 *
 * @param ip ip
 * @return location
 */
@Nullable
IpInfo memorySearch(String ip);

/**
 * IP location search
 *
 * @param ptr ptr
 * @return location
 */
@Nullable
IpInfo getByIndexPtr(long ptr);

/**
 * IP location search
 *
 * @param ip ip
 * @return location
 */
@Nullable
IpInfo btreeSearch(long ip);

/**
 * IP location search
 *
 * @param ip ip
 * @return location
 */
@Nullable
IpInfo btreeSearch(String ip);

/**
 * IP location search
 *
 * @param ip ip
 * @return location
 */
@Nullable
IpInfo binarySearch(long ip);

/**
 * IP location search
 *
 * @param ip ip
 * @return location
 */
@Nullable
IpInfo binarySearch(String ip);
</code>

Properties and Methods of IpInfo

<code>
/** city id */
private Integer cityId;
/** country */
private String country;
/** region */
private String region;
/** province */
private String province;
/** city */
private String city;
/** ISP */
private String isp;
/** region ptr in the db file */
private int dataPtr;
/** Get full address */
public String getAddress();
/** Get full address with ISP */
public String getAddressAndIsp();
</code>

Demo

4. About mica

mica is an open‑source collection of Java components by the author (Dreamlu), including:

Spring Boot starter tool mica-auto .

Spring Boot basic component set mica (utilities, captcha, http, redis, ip2region, xss, etc.) ready to use out of the box.

IoT MQTT protocol mica-mqtt , providing both MQTT client and broker.

Feel free to use it!

6. Related Links

ip2region: https://gitee.com/lionsoul/ip2region

mica: https://gitee.com/596392912/mica

mica-auto: https://gitee.com/596392912/mica-auto

mica-mqtt: https://gitee.com/596392912/mica-mqtt

JavaSpring BootIP Geolocationip2regionMica
Java Architecture Diary
Written by

Java Architecture Diary

Committed to sharing original, high‑quality technical articles; no fluff or promotional content.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.