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