高德API

master
DongZeLiang 2024-04-11 11:43:05 +08:00
parent f39563d4b8
commit 4df1bd25ac
6 changed files with 131 additions and 27 deletions

View File

@ -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();
}

View File

@ -1,9 +0,0 @@
package com.muyu.system.forest.gaode;
/**
* @author DongZl
* @description:
* @Date 2024/4/11 10:49
*/
public interface GaoDeApi {
}

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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");
}
}

View File

@ -2,6 +2,7 @@ package com.forest;
import com.muyu.system.MuYuSystemApplication; import com.muyu.system.MuYuSystemApplication;
import com.muyu.system.forest.api.TestApi; import com.muyu.system.forest.api.TestApi;
import com.muyu.system.forest.gaode.api.GaoDeBaseApi;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
@ -16,11 +17,16 @@ import javax.annotation.Resource;
public class FoRestTest { public class FoRestTest {
@Resource @Resource
private TestApi testApi; private GaoDeBaseApi gaoDeBaseApi;
@Test @Test
public void testApiGaoDe(){ public void district(){
String test = testApi.test(); String test = gaoDeBaseApi.district("奉贤区","3");
System.out.println(test);
}
@Test
public void weather(){
String test = gaoDeBaseApi.weather("310120");
System.out.println(test); System.out.println(test);
} }