housseminfo
housseminfo

Reputation: 107

communication between 2 rest web services

I have many rest controllers in my project (developed with spring MVC) and i would like to make them communicate between each other. what's the best way to make two spring REST controllers exchange messages ?

Upvotes: 2

Views: 6119

Answers (1)

Tuan Hoang
Tuan Hoang

Reputation: 686

Supposed you have 2 controllers:

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

and

@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.
}

For reference, have a look at this: http://www.mkyong.com/java/apache-httpclient-examples/

Used library: https://hc.apache.org/httpcomponents-client-ga/

Upvotes: 2

Related Questions