将JSON POST请求从一个REST API转发到另一个 [英] Forward JSON POST request from one REST API to another

查看:159
本文介绍了将JSON POST请求从一个REST API转发到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况:

我的REST API之一:

My REST API one:

@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:

JSON POST request, request1, for the REST API(Controller1):

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

我的REST API的两个:

My REST API two:

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

针对REST API(Controller2)的JSON请求request2:

JSON request, request2, for the REST API(Controller2):

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

我有几个这样的原始"请求. 现在,我期待一个JSON请求,我们将其称为request3,它是此类原始"查询的组合-如下所示:

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"}]"
   }
}

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

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

推荐答案

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

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

从一种控制器方法重定向到另一种控制器方法

或者,您也可以使用Rest模板调用rest方法 Spring MVC-从另一个内部调用rest服务休息服务

Alternatively you also can call the rest method using Rest Template 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

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

这篇关于将JSON POST请求从一个REST API转发到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆