简体   繁体   English

从POST请求向Java中的外部GET请求发送自定义RequestBody

[英]send custom RequestBody from POST request to an external GET request in java

I'm setting up a rest service (in a microservices scenario) with multi-threaderd post request and I need to differentiate the RequestBody delivery, for example: 我正在使用多线程发布请求设置一个REST服务(在微服务场景中),我需要区分RequestBody传递,例如:

{obj0, obj1, obj2, obj3}

First three obj are correctly forwarder on a kafka queue but now I want to add new obj ( obj3 ) and separate his delivery to send it on an another external rest service. 前三个obj正确地在kafka队列上转发,但是现在我想添加新的obj( obj3 )并分离他的传递,以将其发送到另一个外部rest服务上。 This external GET service will receive obj3 (that are just 2 strings) to valorize his PathVariable and trigger some other algorithms. 此外部GET服务将接收obj3(仅2个字符串),以使他的PathVariable变价并触发其他一些算法。 How can I achive this result? 我如何获得这个结果? In my doExecute() method I've some code like this: 在我的doExecute()方法中,我有一些类似以下的代码:

for (String element : bodyElements) {
  String[] singleElement = element.split(",");

        if (obj1 || obj2 || obj3){
          ...do something;
          }else{
           ...forward obj3 to other REST service
          }

Any help is appreciate, thanks! 任何帮助表示感谢,谢谢!

I hope this will answer your question 我希望这能回答您的问题

HttpEntity body = new HttpEntity(postBody,headers);
new RestTemplate().exchange("http://service-url",HttpMethod.POST,body,String.class);

This is just an example of rest template. 这只是其余模板的示例。 If you want to do GET call just replace HttpMethod.POST to HttpMethod.GET and pass body as null 如果要执行GET调用,只需将HttpMethod.POST替换为HttpMethod.GET并将主体传递为null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM