I have the following situation:
My REST API one:
@RestController
@RequestMapping("/controller1")
Public Class Controller1{
@RequestMapping(method = RequestMethod.POST)
public void process(@RequestBody String jsonString) throws InterruptedException, ExecutionException
{
............
}
}
JSON POST request, request1, for the REST API(Controller1):
{
"key1":"value1",
"key2":"value2"
}
My REST API two:
@RestController
@RequestMapping("/controller2")
Public Class Controller2{
@RequestMapping(method = RequestMethod.POST)
public void process(@RequestBody String jsonString) throws InterruptedException, ExecutionException
{
............
}
}
JSON request, request2, for the REST API(Controller2):
{
"key1":"value1",
"key2":"value2",
"key3":"value3"
}
I have several such "primitive" requests. Now, I am expecting a JSON request, let's call it request3, which is a combination of such "primitive" queries- something that looks like below:
{
{
"requestType":"requestType1",
"request":"[{"key1":"value1","key2":"value2"}]"
},
{
"requestType":"requestType2",
"request":"[{"key1":"value1","key2":"value2","key3":"value3"}]"
}
}
Here, I need to trigger the respective API (one or two) upon identifying the query type. I wanna know how I can forward the request to the corresponding REST API. I wrote the REST API for request3 like below:
@RestController
@RequestMapping("/controller3")
Public Class Controller3{
@RequestMapping(method = RequestMethod.POST)
public void process(@RequestBody String jsonString) throws InterruptedException, ExecutionException
{
..................
..................
switch(request){
case request1: //how to call REST API 1?
case request2: //how to call REST API 2?
}
}
}
You can call a utility method which posts request to controller using Rest Template as below. Since you are using POST method it's easy to send parameters using Rest Template. You may need to edit this code a bit to work in your environment with exact syntax.
Redirect from one controller method to another controller method
Alternatively you also can call the rest method using Rest Template Spring MVC - Calling a rest service from inside another rest service
You may find how to send POST request with params in this post https://techie-mixture.blogspot.com/2016/07/spring-rest-template-sending-post.html