首页 > 解决方案 > 请求 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 之间添加以获取有关股票的信息。

这里有几个问题:

  1. 我知道 CommandLineRunner 会在您运行应用程序时立即启动。什么是让我的应用程序等待用户“输入”的好方法?
  2. 其次,使用 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 等待用户“输入”然后运行。但我似乎无法弄清楚如何做到这一点。

标签: javaspringapi

解决方案


推荐阅读