【发布时间】:2015-07-23 09:23:03
【问题描述】:
我的项目中有很多休息控制器(使用 spring MVC 开发),我想让它们相互通信。 使两个弹簧 REST 控制器交换消息的最佳方法是什么?
【问题讨论】:
标签: web-services rest spring-mvc
我的项目中有很多休息控制器(使用 spring MVC 开发),我想让它们相互通信。 使两个弹簧 REST 控制器交换消息的最佳方法是什么?
【问题讨论】:
标签: web-services rest spring-mvc
假设你有 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/
【讨论】: