Forward JSON POST request from one REST API to another

user_abh picture user_abh · Aug 8, 2016 · Viewed 14.4k times · Source

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?
      }
    }
}

Answer

Tharsan Sivakumar picture Tharsan Sivakumar · Aug 8, 2016

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.

@RequestMapping( value= "/controller3" method = RequestMethod.POST)
 public @ResponseBody void process(@RequestBody String jsonString){

    String request = requestType //Get the request type from request
    String url = "";
    MultiValueMap<String, String> params= null;
    switch(request){
         case request1: //how to call REST API 1?
            url = "/controller1";
            params = request1param //Get the parameter map from request 
         case request2: //how to call REST API 2?
            url = "/controller2";
            params = request2Param //Get the parameter map from request 
      }

      //Now call the method with parameters
      getRESTResponse(url, params);

  }

  private String getRESTResponse(String url, MultiValueMap<String, String> params){
     RestTemplate template = new RestTemplate();
     HttpEntity<MultiValueMap<String, String>> requestEntity= 
            new HttpEntity<MultiValueMap<String, String>>(params);
    String response = "";
     try{
        String responseEntity = template.exchange(url, HttpMethod.POST, requestEntity,  String.class);
        response = responseEntity.getBody();
    }
    catch(Exception e){
        response = e.getMessage();
    }
    return response;
  }

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