在启动类上加上@ForestScan注解,并在basePackages属性中填写远程通达信 行情api通达信 行情api接口,接口,通达信 行情api接口,接口所在的包名
// forest扫描远程通达信 行情api接口,接口所在的包名
@ForestScan(basePackages = 'com.zhengqing.demo.rpc.client')
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
今日分享语句:能力配不上野心,是所有烦扰的根源。这个世界是公平的,你要想得到,就得学会付出和坚持。每个人都是通过自己的努力,去决定生活的样子。
applicatioyml中相关配置
# ========================== ↓↓↓↓↓↓ forest配置 ↓↓↓↓↓↓ ==========================
forest:
bean-id: config0 # 在spring上下文中bean的id, 默认值为forestConfiguration
backend: okhttp3 # 后端HTTP API: okhttp3 【支持`okhttp3`/`httpclient`】
max-connections: 1000 # 连接池最大连接数,默认值为500
max-route-connections: 500 # 每个路由的最大连接数,默认值为500
timeout: 3000 # 请求超时时间,单位为毫秒, 默认值为3000
connect-timeout: 3000 # 连接超时时间,单位为毫秒, 默认值为2000
retry-count: 0 # 请求失败后重试次数,默认为0次不重试
ssl-protocol: SSLv3 # 单向验证的HTTPS的默认SSL协议,默认为SSLv3
logEnabled: true # 打开或关闭日志,默认为true
Forest是一个高层的、极简的轻量级HTTP调用API框架,让Java发送HTTP/HTTPS请求不再难。它比OkHttp和HttpClient更高层,比Feign更轻量,是封装调用第三方restfulapiclient接口的好帮手。
构建请求接口
SpringBoot整合Forest实现调用第三方接口
poxml中引入依赖
com.dtflys.forest
spring-boot-starter-forest
1.4.0
Forest是什么?
相比于直接使用Httpclient我们不再写一大堆重复的代码了,而是像调用本地方法一样去发送HTTP请求。
温馨小提示:下面只是简单请求模拟测试,更多可参考官方文档!
public interface MyClient {
/**
* 本地测试接口
*/
@Get(url = 'http://127.0.0.1:80/demo/index')
String index();
@Get(url = 'http://127.0.0.1:80/demo/hello?msg=${msg}')
String hello(@DataVariable('msg') String msg);
/**
* 高德地图API
*/
@Get(url = 'http://ditu.amap.com/service/regeo?longitude=${longitude}&latitude=${latitude}')
Map getLocation(@DataVariable('longitude') String longitude, @DataVariable('latitude') String latitude);
}
配置扫描接口
测试
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoTest {
@Autowired
private MyClient myClient;
@Test
public void testForest() throws Exception {
// 调用接口
String index = myClient.index();
log.info('index: 【{}】', index);
String hello = myClient.hello('测试...');
log.info('hello: 【{}】', hello);
Map location = myClient.getLocation('121.475078', '31.223577');
log.info('location: 【{}】', location.toString());
}
}
文章为作者独立观点,不代表股票交易接口观点