我有一个REST服务和一个外部服务器,https://api.myrestservice.com
并且我有一个Spring Boot Application在本地运行http://localhost:8080
。现在,我想使用本地运行的Spring Boot App(即通过)向REST API地址发出GET或POST请求,即https://api.myrestservice.com/users
获取所有用户http://localhost:8080/users
。我没有得到如何将本地应用程序请求重定向到外部服务器请求。
希望我能正确回答你的问题。您正在尝试获取本地应用,以从服务器上运行的应用获取数据。
您可以在spring boot应用程序中使用以下示例代码。
private void getUsers() { final String uri = "https://api.myrestservice.com/users"; RestTemplate restTemplate = new RestTemplate(); Users result = restTemplate.getForObject(uri, Users.class); System.out.println(result); }
然后,您的getUsers可以由您的spring boot应用程序中的getUsers Controller调用。
如果您想查看更多示例,请添加参考-https: //howtodoinjava.com/spring-restful/spring-restful-client-resttemplate-example/
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句