2个休息网络服务之间的通信

时间:2015-05-12 13:11:36

标签: web-services rest spring-mvc

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

1 个答案:

答案 0 :(得分: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/