# 一、通过手机号获取归属地和运营商
# 1.1 创建 SpringBoot 工程
# 1.2 引入依赖
<properties> | |
<java.version>21</java.version> | |
<libphonenumber.version>8.13.33</libphonenumber.version> | |
<libphonenumber.carrier>1.214</libphonenumber.carrier> | |
<libphonenumber.geocoder>2.221</libphonenumber.geocoder> | |
<fastjson.version>1.2.28</fastjson.version> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>com.alibaba</groupId> | |
<artifactId>fastjson</artifactId> | |
<version>${fastjson.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-web</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.projectlombok</groupId> | |
<artifactId>lombok</artifactId> | |
<optional>true</optional> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-test</artifactId> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>com.googlecode.libphonenumber</groupId> | |
<artifactId>libphonenumber</artifactId> | |
<version>${libphonenumber.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>com.googlecode.libphonenumber</groupId> | |
<artifactId>geocoder</artifactId> | |
<version>${libphonenumber.geocoder}</version> | |
</dependency> | |
<dependency> | |
<groupId>com.googlecode.libphonenumber</groupId> | |
<artifactId>carrier</artifactId> | |
<version>${libphonenumber.carrier}</version> | |
</dependency> | |
</dependencies> |
# 二、代码编写
# 2.1 视图层
@RestController | |
@RequestMapping("/phone") | |
public class PhoneController | |
{ | |
@GetMapping("getPhone") | |
public R getPhone(String phone) | |
{ | |
return R.success(PhoneToRegionUtils.getPhone(phone)); | |
} | |
} |
# 2.2 工具类
public class PhoneToRegionUtils | |
{ | |
// 手机号基本工具类 | |
private final static PhoneNumberUtil PHONE_NUMBER_UTIL = PhoneNumberUtil.getInstance(); | |
// 运营商 | |
private final static PhoneNumberToCarrierMapper CARRIER_MAPPER = PhoneNumberToCarrierMapper.getInstance(); | |
private final static PhoneNumberOfflineGeocoder GEO_CODER = PhoneNumberOfflineGeocoder.getInstance(); | |
/** | |
* 获取手机号的归属信息:运营商,归属地 | |
* @param phone 手机号 | |
* @return 归属信息 | |
*/ | |
public static JSONObject getPhone(String phone) | |
{ | |
JSONObject affiliation = new JSONObject(); | |
affiliation.put("phone", phone); | |
affiliation.put("carrier", getPhoneCarrier(phone)); | |
affiliation.put("region", getRegionInfoByPhone(phone)); | |
return affiliation; | |
} | |
/** | |
* 获取手机号归属地 | |
* @param phone 手机号 | |
* @return 归属地 | |
*/ | |
private static String getRegionInfoByPhone(String phone) | |
{ | |
return isValidNumber(phone) ? GEO_CODER.getDescriptionForNumber(getPhoneNumber(phone), Locale.CHINESE) : ""; | |
} | |
public static String getPhoneCarrier(String phone) | |
{ | |
return isValidNumber(phone) ? CARRIER_MAPPER.getNameForNumber(getPhoneNumber(phone), Locale.CHINA) : ""; | |
} | |
/** | |
* 验证当前手机号是否有效 | |
* @param phone 手机号 | |
* @return 校验结果 | |
*/ | |
public static boolean isValidNumber(String phone) | |
{ | |
return PHONE_NUMBER_UTIL.isValidNumber(getPhoneNumber(phone)); | |
} | |
/** | |
* 生成 PhoneNumber | |
* @param phone 手机号 | |
* @return PhoneNumber | |
*/ | |
private static Phonenumber.PhoneNumber getPhoneNumber(String phone) | |
{ | |
Phonenumber.PhoneNumber phoneNumber = new Phonenumber.PhoneNumber(); | |
phoneNumber.setCountryCode(86); | |
phoneNumber.setNationalNumber(Long.parseLong(phone)); | |
return phoneNumber; | |
} | |
} |
测试结果: