利用Openfeign远程调用第三方接口(案例:百度地图逆地理编码接口,实现通过经纬度坐标获取详细地址)
文章目录
- 1、注册百度地图账号
- 2、编写代码
- 2.1、添加openfeign依赖
- 2.2、主启动类加注解@EnableFeignClients
- 2.3、写远程调用的OpenFeign接口
- 3、测试&响应结果
- 4、添加feign日志&注意事项
- 4.1、添加日志
- 4.2、注意事项
1、注册百度地图账号
https://lbsyun.baidu.com/apiconsole/key
获取AK(COPY出来,以后要用)
查看百度地图逆地理解析接口文档:
PI服务地址
https://api.map.baidu.com/reverse_geocoding/v3/?ak=您的ak&extensions_poi=1&entire_poi=1&sort_strategy=distance&output=json&coordtype=bd09ll&location=39.951335108535, 116.51484487905
//GET请求
请求地址:
get: https://api.map.baidu.com/reverse_geocoding/v3/
请求参数如下:
key | value |
---|---|
ak: | “PRyu1SQEdG4rihx0RDxxxxxxx”, |
output: | “json”, |
extensionsPoi: | “1”, |
location: | “31.225696563611,121.49884033194” |
参数说明:
ak:百度地图 API Key(需要自己申请)
output:输出格式(json/xml)
extensions_poi 是一个 扩展参数,用于返回周边的 POI(兴趣点,Points of Interest) 信息,例如餐馆、商店、学校等。
location:经纬度(格式 lat,lng)
2、编写代码
2.1、添加openfeign依赖
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency>
2.2、主启动类加注解@EnableFeignClients
@EnableFeignClients
//@EnableDiscoveryClient //核心注解
@SpringBootApplication
public class OrderMainApplication {public static void main(String[] args) {SpringApplication.run(OrderMainApplication.class, args);}
}
2.3、写远程调用的OpenFeign接口
package com.tigerhhzz.order.openfeign;import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;/*** @Author tigerhhzz* @Date 2025 03 25 13 00**/
@FeignClient(value = "baidumap-client",url = "http://api.map.baidu.com")
public interface BaiduMapOpenfeign {@GetMapping("/reverse_geocoding/v3")String getLocationByLonLat(@RequestParam String ak, @RequestParam String output, @RequestParam String extensionsPoi, @RequestParam String location);}
3、测试&响应结果
package com.tigerhhzz.order;import com.tigerhhzz.order.openfeign.BaiduMapOpenfeign;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;/*** @Author tigerhhzz* @Date 2025 03 25 13 07**/
@SpringBootTest
public class BaidumapTest {@AutowiredBaiduMapOpenfeign baiduMapOpenfeign;@Testvoid test01(){String location = baiduMapOpenfeign.getLocationByLonLat("PRyu1SQEdG4rihxxxxxxS8NIcsc", "json", "1", "31.225696563611,121.49884033194");System.out.println(location);}
}
响应结果(包含 POI 信息):
{"status": 0,"result": {"location": {"lng": 121.49884033193993,"lat": 31.225696429417988},"formatted_address": "上海市黄浦区净土街31弄-4号","edz": {"name": "陆家嘴金融贸易区"},"business": "老西门,城隍庙,西藏南路","business_info": [{"name": "老西门","location": {"lng": 121.49270784559798,"lat": 31.22284211295233},"adcode": 310101,"distance": 0,"direction": "内"},{"name": "城隍庙","location": {"lng": 121.49754405297205,"lat": 31.231853482990294},"adcode": 310101,"distance": 394,"direction": "南"},{"name": "西藏南路","location": {"lng": 121.49155262471214,"lat": 31.219955867050826},"adcode": 310101,"distance": 629,"direction": "东北"}],"addressComponent": {"country": "中国","country_code": 0,"country_code_iso": "CHN","country_code_iso2": "CN","province": "上海市","city": "上海市","city_level": 2,"district": "黄浦区","town": "老西门街道","town_code": "310101019","distance": "58","direction": "北","adcode": "310101","street": "净土街","street_number": "31弄-4号"},"pois": [],"roads": [],"poiRegions": [],"sematic_description": "","formatted_address_poi": "","cityCode": 289}
}
formatted_address:地址信息
pois:POI 兴趣点列表
name:兴趣点名称
addr:兴趣点地址
point:兴趣点坐标
distance:距离查询点的距离(单位:米)
tag:类别(如景点、商场、餐厅等)
4、添加feign日志&注意事项
4.1、添加日志
在配置文件application.yml中添加:
logging:level:com.tigerhhzz.order.openfeign: debug
然后新建如下配置类:
package com.tigerhhzz.order.config;import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;@Configuration
public class OrderServiceConfig {@Beanpublic RestTemplate restTemplate(){return new RestTemplate();}@BeanLogger.Level feignLoggerLevel(){return Logger.Level.FULL;}
}
日志输出:
4.2、注意事项
在百度地图控制台,相应的api接口中加上发送请求主机的ip白名单;否则会提示如下检验失败的信息:
真正的强大不是忘记,而是接受