java - 请求 API url 时添加用户输入
问题描述
这是我正在进行的一个项目,主要是为了获取某些股票的信息。原来的工作代码是:
public static CommandLineRunner run(RestTemplate restTemplate) throws Exception {
return args -> {
Data data = restTemplate.getForObject(
"https://finnhub.io/api/v1/stock/insider-transactions?symbol=CRM&token=c683tuqad3iagio36uj0", Data.class);
log.info(data.toString());
System.out.println(data);
ResponseEntity<List<String>> peersResponse =
restTemplate.exchange("https://finnhub.io/api/v1/stock/peers?symbol=CRM&token=c683tuqad3iagio36uj0",
HttpMethod.GET, null, new ParameterizedTypeReference<List<String>>() {
});
List<String> peers = peersResponse.getBody();
System.out.println(peers);
};
}
而我正在尝试做的是添加一个用户“输入”以在 URL 之间添加以获取有关股票的信息。
这里有几个问题:
- 我知道 CommandLineRunner 会在您运行应用程序时立即启动。什么是让我的应用程序等待用户“输入”的好方法?
- 其次,使用 API url 中的“输入”来请求数据?
我不确定我是否正确地解释了自己,我希望通过展示我的原始代码和我想要实现的代码将使任何专家更容易理解。谢谢你的帮助!
我想做的事:
public static CommandLineRunner run(RestTemplate restTemplate) throws Exception {
return args -> {
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
Data data = restTemplate.getForObject(
"https://finnhub.io/api/v1/stock/insider-transactions?symbol=" + input + "&token=c683tuqad3iagio36uj0", Data.class);
log.info(data.toString());
System.out.println(data);
ResponseEntity<List<String>> peersResponse =
restTemplate.exchange("https://finnhub.io/api/v1/stock/peers?symbol=" + input + "&token=c683tuqad3iagio36uj0",
HttpMethod.GET, null, new ParameterizedTypeReference<List<String>>() {
});
List<String> peers = peersResponse.getBody();
System.out.println(peers);
};
}
我刚刚修改了我的代码以正确接受 API URL 的“输入”,并显示了我的更多代码:
public class StocksQueryApplication {
private static final Logger log = LoggerFactory.getLogger(StocksQueryApplication.class);
private static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
SpringApplication.run(StocksQueryApplication.class, args);
}
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
@Bean
public static CommandLineRunner run(RestTemplate restTemplate) throws Exception {
return args -> {
Data data = restTemplate.getForObject(
"https://finnhub.io/api/v1/stock/insider-transactions?symbol=" + input + "&token=c683tuqad3iagio36uj0", Data.class);
log.info(data.toString());
System.out.println(data);
ResponseEntity<List<String>> peersResponse =
restTemplate.exchange("https://finnhub.io/api/v1/stock/peers?symbol=" + input + "&token=c683tuqad3iagio36uj0",
HttpMethod.GET, null, new ParameterizedTypeReference<List<String>>() {
});
List<String> peers = peersResponse.getBody();
System.out.println(peers);
};
}
}
剩下的就是让 CommandLineRunner 等待用户“输入”然后运行。但我似乎无法弄清楚如何做到这一点。
解决方案
推荐阅读
- php - 函数 wp_update_post() 为某些帖子返回 0
- julia - 为什么 Julia 开发人员将 `NA` 重命名为 `missing`
- python - 从十六进制到二进制并删除二进制数字
- javascript - 将数据从控制台传递到 URL
- python - 如何获得每个可能的组合固定长度字符串
- docker - 如何在生产中的服务之间共享源代码?
- linux - 如何删除文件中 int 值之间的间隙?
- sql - 包含 __ 的 SQL 计数列,没有重复?
- python - 为什么我的正则表达式不能在网页上接听电话号码?
- python - 将不同长度列表的 dict 值转换为一个列表,然后将该列表添加到 Dataframe