参考
https://dt_flys.gitee.io/forest
非 Spring Boot 项目中安装
Maven 依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>3.3.0</version> </dependency>
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency>
<dependency> <groupId>com.dtflys.forest</groupId> <artifactId>forest-core</artifactId> <version>1.3.2</version> </dependency>
|
Spring Boot
Maven 依赖
1 2 3 4 5
| <dependency> <groupId>com.dtflys.forest</groupId> <artifactId>spring-boot-starter-forest</artifactId> <version>1.3.2</version> </dependency>
|
创建一个接口
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.yoursite.client;
import com.dtflys.forest.annotation.Request; import com.dtflys.forest.annotation.DataParam;
public interface AmapClient {
@Request( url = "http://ditu.amap.com/service/regeo?longitude=${0}&latitude=${1}", dataType = "json" ) Map getLocation(String longitude, String latitude); }
|
扫描接口
在 Spring Boot 的配置类或者启动类上加上 @ForestScan
注解,并在 basePackages
属性里填上接口的所在的包名
1 2 3 4 5 6 7 8
| @SpringBootApplication @Configuration @ForestScan(basePackages = "com.yoursite.client") public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } }
|
调用接口
1 2 3 4 5 6 7
| // 注入接口实例 @Autowired private AmapClient amapClient; ... // 调用接口 Map result = amapClient.getLocation("121.475078", "31.223577"); System.out.println(result);
|