I am learning to implement an application using Angular and Spring Boot. Our application uses Angular for front end and Spring Boot Rest Services in the back end. I am looking for a way to pull some data based on an Id from Rest Service 1 (Invoking the RestController1) and calling the Rest Service 2 from Rest Service 1 for corresponding data for the data pulled from RestController1. How can I call RestController2 from RestController1 and get the data. We are using hibernate. Please let me know if there is any spring reference documentation to read and understand the implementation. Any help is greatly appreciated.
Thanks
Invoking one controller from another sounds like a bad idea for a couple of reasons:
It's your application and you should consider aggregating all the data you need on service layer, not on the controller. so controller can get all the required data in one service method call.
You really don't want that http request - all the serialization, DNS calling, http request invocation, deserialization - all that somehow impacts performance and if you can avoid using it, don't use it :)
Even if performance impact does not bother you (because it is insignificant), don't forget that network is unreliable
If the RestController2 that you want to call from RestController1 should only be accessible by application, you should provide security to it while this can be redundant
Even if you still want to call another controller, consider only invoking it's method (without any http requests), since it's just a regular class's method.
There are many ways to do this. One of those is use RestTemplate from spring:
private static void getEmployees()
{
final String uri = "http://localhost:8080/springrestexample/employees";
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject(uri, String.class);
System.out.println(result);
}
See the docs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With