【问题标题】:communication between 2 rest web services2个REST Web服务之间的通信
【发布时间】:2015-07-23 09:23:03
【问题描述】:

我的项目中有很多休息控制器(使用 spring MVC 开发),我想让它们相互通信。 使两个弹簧 REST 控制器交换消息的最佳方法是什么?

【问题讨论】:

    标签: web-services rest spring-mvc


    【解决方案1】:

    假设你有 2 个控制器:

    @RestController
    @RequestMapping("/first")
    public class FirstController(){
    // your code here
    }
    

    @RestController
    @RequestMapping("/second")
    public class SecondController(){
        // supposed this is your FirstController url.
        String url = "http://localhost:8080/yourapp/first";
        // create request.
        HttpClient client = HttpClientBuilder.create().build();
        HttpGet request = new HttpGet(url);
        // execute your request.
        HttpResponse response = client.execute(request);
       // do whatever with the response.
    }
    

    作为参考,看看这个:http://www.mkyong.com/java/apache-httpclient-examples/

    使用的库:https://hc.apache.org/httpcomponents-client-ga/

    【讨论】:

    • 谢谢@Tuan 这种方式非常有用。但是你能告诉我 2 个控制器是如何在不修改其代码的情况下交换消息的吗?我需要编排或中间件之类的东西吗?
    • 对不起,我想不出更好的主意... :( 我认为您可以使用 SecondController 调用 FirstController 来获得您想要的东西。
    猜你喜欢
    • 1970-01-01
    • 2019-01-18
    • 1970-01-01
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    • 2010-09-29
    • 1970-01-01
    • 2022-09-30
    相关资源
    最近更新 更多