ip2region 是准确率 99.9% 的 IP 地址定位库,0.0x毫秒级查询,提供了 Java、PHP、C、Python、Node.js、Golang、C#、Rust、Lua的查询绑定和 Binary、B树、内存三种查询算法!
-
net.dreamlu -
mica-ip2region -
2.5.6 -
cn.hutool -
hutool-all -
5.8.11
提示:Ip2regionSearcher的和hutool工具包依赖包,这里导入hutool为了后期利用获取IP工具类
- import cn.hutool.extra.servlet.ServletUtil;
- import lombok.RequiredArgsConstructor;
- import net.dreamlu.mica.ip2region.core.Ip2regionSearcher;
- import net.dreamlu.mica.ip2region.core.IpInfo;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RestController;
- import javax.servlet.http.HttpServletRequest;
-
- /**
- *
- * 测试控制器
- *
- *
- * @className: XController
- * @author: BertXie
- * @create: 2023-01-09 11:40
- */
-
- @RestController
- @RequiredArgsConstructor
- public class XController {
-
- private final Ip2regionSearcher searchSearcher;
-
- @GetMapping("/ip")
- public String getHomeLocation(HttpServletRequest request) {
- IpInfo ipInfo = searchSearcher.memorySearch(ServletUtil.getClientIP(request));
- return ipInfo != null ? ipInfo.getCity() : "未知星球";
- }
-
- }
注意:这里注入了Ip2regionSearcher对象,并使用了memorySearch方法,会返回一个IP详情对象,还用到了Hutool工具的获取IP的方法ServletUtil.getClientIP

提示:这里我是写的接口,你也可以直接写一个测试类,填入IP即可