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

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

最佳答案

您可以调用一个实用方法,该方法使用 Rest 模板向 Controller 发布请求,如下所示。由于您使用的是 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

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

关于json - 将 JSON POST 请求从一个 REST API 转发到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38829595/

相关文章:

java - 捕获 Spring Integration DSL 上的错误

java - JPA/Hibernate 删除实体中的所有内容

android - 如何返回与 url django rest 中具有相同名字或姓氏的所有用户实例?

javascript - 如何在不按回车按钮或提交按钮的情况下输入时获取搜索结果?

javascript - 在 Javascript 中访问 JavaScriptSerializer

javascript - ES6 - 遍历 JSON

javascript - 找不到入口模块中的错误 : Error: Can't resolve 'babel-loader'

JAVA_HOME 应该指向 JDK 而不是 JRE + JAVA_HOME 未正确定义

javascript - 在 JSON 中编码日期的最佳实践是什么?

node.js - 相互通信微服务 - 如何?