高德API
parent
f39563d4b8
commit
4df1bd25ac
|
@ -1,15 +0,0 @@
|
|||
package com.muyu.system.forest.api;
|
||||
|
||||
import com.dtflys.forest.annotation.Get;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 测试
|
||||
* @Date 2024/4/11 上午10:34
|
||||
*/
|
||||
public interface TestApi {
|
||||
|
||||
|
||||
@Get("https://restapi.amap.com/v3/config/district?keywords=中国&subdistrict=3&key=9d17aa33a6d6d32bd34944f0d8ae60cf")
|
||||
public String test();
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
package com.muyu.system.forest.gaode;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 高德地图接口
|
||||
* @Date 2024/4/11 上午10:49
|
||||
*/
|
||||
public interface GaoDeApi {
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.muyu.system.forest.gaode.api;
|
||||
|
||||
import com.dtflys.forest.annotation.BaseRequest;
|
||||
import com.dtflys.forest.annotation.Get;
|
||||
import com.dtflys.forest.annotation.Var;
|
||||
import com.muyu.system.forest.gaode.interceptor.GaoDeInterceptor;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 高德地图接口
|
||||
* @Date 2024/4/11 上午10:49
|
||||
*/
|
||||
@BaseRequest(
|
||||
baseURL = "https://restapi.amap.com",
|
||||
interceptor = GaoDeInterceptor.class
|
||||
)
|
||||
public interface GaoDeBaseApi {
|
||||
|
||||
@Get("/v3/config/district?keywords={keywords}&subdistrict={subDistrict}")
|
||||
String district(@Var("keywords") String keywords, @Var("subDistrict") String subDistrict);
|
||||
|
||||
@Get("/v3/weather/weatherInfo?city={city}")
|
||||
String weather(@Var("city") String city);
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.muyu.system.forest.gaode.confg;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 高德配置
|
||||
* @Date 2024/4/11 上午11:30
|
||||
*/
|
||||
@Data
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "gaode")
|
||||
public class GaoDeConfig {
|
||||
|
||||
/**
|
||||
* 高德Key
|
||||
*/
|
||||
private String key;
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
package com.muyu.system.forest.gaode.interceptor;
|
||||
|
||||
import com.dtflys.forest.converter.ForestEncoder;
|
||||
import com.dtflys.forest.exceptions.ForestRuntimeException;
|
||||
import com.dtflys.forest.http.ForestRequest;
|
||||
import com.dtflys.forest.http.ForestResponse;
|
||||
import com.dtflys.forest.interceptor.Interceptor;
|
||||
import com.dtflys.forest.reflection.ForestMethod;
|
||||
import com.muyu.system.forest.gaode.confg.GaoDeConfig;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Log4j2
|
||||
@Component
|
||||
public class GaoDeInterceptor implements Interceptor<String> {
|
||||
|
||||
@Autowired
|
||||
private GaoDeConfig gaoDeConfig;
|
||||
/**
|
||||
* 该方法在被调用时,并在beforeExecute前被调用
|
||||
* @Param request Forest请求对象
|
||||
* @Param args 方法被调用时传入的参数数组
|
||||
*/
|
||||
@Override
|
||||
public void onInvokeMethod(ForestRequest req, ForestMethod method, Object[] args) {
|
||||
log.info("on invoke method");
|
||||
}
|
||||
|
||||
/**
|
||||
* 在请求体数据序列化后,发送请求数据前调用该方法
|
||||
* 默认为什么都不做
|
||||
* 注: multlipart/data类型的文件上传格式的 Body 数据不会调用该回调函数
|
||||
*
|
||||
* @param request Forest请求对象
|
||||
* @param encoder Forest转换器
|
||||
* @param encodedData 序列化后的请求体数据
|
||||
*/
|
||||
public byte[] onBodyEncode(ForestRequest request, ForestEncoder encoder, byte[] encodedData) {
|
||||
// request: Forest请求对象
|
||||
// encoder: 此次转换请求数据的序列化器
|
||||
// encodedData: 序列化后的请求体字节数组
|
||||
// 返回的字节数组将替换原有的序列化结果
|
||||
// 默认不做任何处理,直接返回参数 encodedData
|
||||
return encodedData;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 该方法在请求发送之前被调用, 若返回false则不会继续发送请求
|
||||
* @Param request Forest请求对象
|
||||
*/
|
||||
@Override
|
||||
public boolean beforeExecute(ForestRequest req) {
|
||||
log.info("invoke Simple beforeExecute");
|
||||
// 执行在发送请求之前处理的代码 // 添加Header
|
||||
req.addQuery("key", gaoDeConfig.getKey()); // 添加URL的Query参数
|
||||
return true; // 继续执行请求返回true
|
||||
}
|
||||
|
||||
/**
|
||||
* 该方法在请求发送失败时被调用
|
||||
*/
|
||||
@Override
|
||||
public void onError(ForestRuntimeException ex, ForestRequest req, ForestResponse res) {
|
||||
log.info("invoke Simple onError");
|
||||
}
|
||||
|
||||
/**
|
||||
* 该方法在请求发送之后被调用
|
||||
*/
|
||||
@Override
|
||||
public void afterExecute(ForestRequest req, ForestResponse res) {
|
||||
log.info("invoke Simple afterExecute");
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@ package com.forest;
|
|||
|
||||
import com.muyu.system.MuYuSystemApplication;
|
||||
import com.muyu.system.forest.api.TestApi;
|
||||
import com.muyu.system.forest.gaode.api.GaoDeBaseApi;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
|
@ -16,11 +17,16 @@ import javax.annotation.Resource;
|
|||
public class FoRestTest {
|
||||
|
||||
@Resource
|
||||
private TestApi testApi;
|
||||
private GaoDeBaseApi gaoDeBaseApi;
|
||||
|
||||
@Test
|
||||
public void testApiGaoDe(){
|
||||
String test = testApi.test();
|
||||
public void district(){
|
||||
String test = gaoDeBaseApi.district("奉贤区","3");
|
||||
System.out.println(test);
|
||||
}
|
||||
@Test
|
||||
public void weather(){
|
||||
String test = gaoDeBaseApi.weather("310120");
|
||||
System.out.println(test);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue