I am trying to call a service in another Spring Boot application using RestTemplate,like below:
@RestController
public class Controller {
@Autowired
RestTemplate restTemplate;
@RequestMapping("/hello")
public String getHi() {
return restTemplate.getForObject("http://localhost:8082/hello", String.class);
}
}
This is the Main class for application A:
@SpringBootApplication
public class ServiceApplication {
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class, args);
}
}
Below the other Spring Boot application B:
@RestController
public class Controller {
@RequestMapping ("/hello")
public String getHi() {
return "Hi";
}
}
Application A (RestTemplate): In this I am trying to achieve the API call to go through like, Postman -> Application A -> Application B. I tried this API with Postman, but the request is not sent. Just showing "Sending request...". Also didn't get failure response.
Application B: Called GET endpoint from Postman which invokes getHi() in
Controller(RestController) class in Postman and browser, both work fine.
Port used:
- Application A: 8083
- Application B: 8082
Both applications are running. I have gone through lots of websites and git codes, but couldn't find a solution.