spring - Forward JSON POST request from one REST API to another -


i have 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, 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, rest api(controller2):

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

i have several such "primitive" requests. now, expecting json request, let's call request3, combination of such "primitive" queries- looks below:

{    {       "requesttype":"requesttype1",       "request":"[{"key1":"value1","key2":"value2"}]"    },    {       "requesttype":"requesttype2",       "request":"[{"key1":"value1","key2":"value2","key3":"value3"}]"    } } 

here, need trigger respective api (one or two) upon identifying query type. wanna know how can forward request corresponding rest api. wrote rest api request3 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 call rest api 1?          case request2: //how call rest api 2?       }     } } 

you can call utility method posts request controller using rest template below. since using post method it's easy send parameters using rest template. may need edit code bit work in environment exact syntax.

@requestmapping( value= "/controller3" method = requestmethod.post)  public @responsebody void process(@requestbody string jsonstring){      string request = requesttype //get request type request     string url = "";     multivaluemap<string, string> params= null;     switch(request){          case request1: //how call rest api 1?             url = "/controller1";             params = request1param //get parameter map request           case request2: //how call rest api 2?             url = "/controller2";             params = request2param //get parameter map request        }        //now call method 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 1 controller method controller method

alternatively can call rest method using rest template spring mvc - calling rest service inside rest service

you may find how send post request params in post https://techie-mixture.blogspot.com/2016/07/spring-rest-template-sending-post.html


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -