将JSON POST请求从一个REST API转发到另一个REST API

时间:2016-08-08 12:44:34

标签: json spring rest spring-boot request-mapping

我有以下情况:

我的REST API:

@RestController
@RequestMapping("/controller1")
Public Class Controller1{
    @RequestMapping(method = RequestMethod.POST)
    public void process(@RequestBody String jsonString) throws InterruptedException, ExecutionException 
    {
       ............
    }
}

REST API(Controller1)的JSON POST请求request1:

{
  "key1":"value1",
  "key2":"value2"
}

我的REST API二:

@RestController
@RequestMapping("/controller2")
Public Class Controller2{
    @RequestMapping(method = RequestMethod.POST)
    public void process(@RequestBody String jsonString) throws InterruptedException, ExecutionException 
    {
       ............
    }
}

REST API(Controller2)的JSON请求request2:

{
  "key1":"value1",
  "key2":"value2",
  "key3":"value3"
}

我有几个这样的"原始"要求。 现在,我期待一个JSON请求,让我们称之为request3,这是这种"原语"的组合。查询 - 如下所示:

{
   {
      "requestType":"requestType1",
      "request":"[{"key1":"value1","key2":"value2"}]"
   },
   {
      "requestType":"requestType2",
      "request":"[{"key1":"value1","key2":"value2","key3":"value3"}]"
   }
}

在这里,我需要在识别查询类型时触发相应的API(一个或两个)。我想知道如何将请求转发到相应的REST API。我为request3编写了REST API,如下所示:

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

1 个答案:

答案 0 :(得分:3)

您可以调用一个实用程序方法,使用Rest Template将请求发送到控制器,如下所示。由于您使用的是POST方法,因此使用Rest Template可以轻松发送参数。您可能需要稍微编辑此代码,以便在您的环境中使用确切的语法。

@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

或者,您也可以使用Rest Template调用rest方法 Spring MVC - Calling a rest service from inside another rest service

您可以在本文中找到如何使用params发送POST请求 https://techie-mixture.blogspot.com/2016/07/spring-rest-template-sending-post.html