问题描述
我有一个像https://api.myrestservice.com
这样的外部服务器上的REST服务,并且我有一个在http://localhost:8080
上本地运行的Spring Boot应用程序.现在,我想使用本地运行的Spring Boot App(即通过http://localhost:8080/users
)向REST API地址(即https://api.myrestservice.com/users
)发出 GET 或 POST 请求,以获取所有用户.我不知道如何将本地应用程序请求重定向到外部服务器请求.
I have a REST Service an external server like https://api.myrestservice.com
and I have a Spring Boot Application running locally on http://localhost:8080
. Now I want to make GET or POST request to the REST API address i.e https://api.myrestservice.com/users
to get all users, using my locally running Spring Boot App i.e through http://localhost:8080/users
. I am not getting how to redirect local app request to external server request.
推荐答案
我希望我能正确回答您的问题.您正在尝试获取本地应用程序,以从服务器上运行的应用程序获取数据.
I hope I got your question right. You are trying get your local app to get data from app running on your server.
您可以在Spring Boot应用程序中使用以下示例代码.
You can use the below sample code in your spring boot application.
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调用.
Then your getUsers can be invoked by getUsers Controller in your spring boot app.
如果您想查看更多示例,请添加参考- https://howtodoinjava.com/spring-restful/spring-restful -client-resttemplate-example/
I am adding the reference if you want to look at more examples -https://howtodoinjava.com/spring-restful/spring-restful-client-resttemplate-example/
这篇关于使用Spring Boot获得REST API的GET/POST Requst的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!