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.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
This is very Simple By using Java Clients you can Use RestTemplate or UniRest That one running on Remote is simply Producer and the one which is in local is Consumer So you can exchange method of Resttemplate or get method of Unirest Example Code is here.
For Unirest code is like this
Making post Api call from your code to another server:
suppose you have a server https://searchEmployee... which returns you list of employees belonging to a particular city or belonging to a particular organization:
request body:
json response:
[{"name": "Vikash"},{"name":"kumar" },{}...etc]
Then to make a post api call you can use RestTemplate in java like this:
/********************************************************************/
if you have more than one request body parameters to set do it like this:
false is a boolean value here in your request body and 100 is an integer.
NOTE if you are having problem in setting request body copy it directly from postman request body and paste it inside double quote.
I hope I got your question right. You are trying get your local app to get data from app running on your server.
You can use the below sample code in your spring boot application.
Then your getUsers can be invoked by getUsers Controller in your spring boot app.
I am adding the reference if you want to look at more examples - https://howtodoinjava.com/spring-restful/spring-restful-client-resttemplate-example/
There are many ways to do it. Like Apache HTTP Components and other. Sample
There are a couple of thing going on here, Like URLEncoding is really mattered when came to security. Note: Source of above code:here.