To write a REST API method in Spring that calls another remote API, you can use the RestTemplate or the newer WebClient (from Spring WebFlux) for making HTTP requests to external services.
Below is a simple example using both approaches:
✅ Prerequisites
Make sure your project has these dependencies (if using Maven):
<!-- For Spring Web (MVC) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Optional: For WebClient support -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
🧱 1. Using RestTemplate (Synchronous)
Step 1: Create a Service Class
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ExternalApiService {
private final RestTemplate restTemplate;
public ExternalApiService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public String callExternalApi() {
String url = "https://jsonplaceholder.typicode.com/posts/1";
return restTemplate.getForObject(url, String.class);
}
}
Step 2: Create a Controller
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ApiController {
private final ExternalApiService externalApiService;
public ApiController(ExternalApiService externalApiService) {
this.externalApiService = externalApiService;
}
@GetMapping("/call-api")
public String getFromExternalApi() {
return externalApiService.callExternalApi();
}
}
Step 3: Enable RestTemplate
If you're not already creating it as a bean, define it in configuration:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class AppConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
🔁 2. Using WebClient (Asynchronous / Non-blocking)
Step 1: Create a Service Class
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
@Service
public class ExternalApiService {
private final WebClient webClient;
public ExternalApiService(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.baseUrl("https://jsonplaceholder.typicode.com").build();
}
public String callExternalApi() {
return webClient.get()
.uri("/posts/1")
.retrieve()
.bodyToMono(String.class)
.block(); // blocking call for simplicity
}
}
⚠️ Use .block() only if you're working in a synchronous context. Otherwise, keep it reactive with Mono<String>.
Step 2: Update the Controller Same as Before
@RestController
public class ApiController {
private final ExternalApiService externalApiService;
public ApiController(ExternalApiService externalApiService) {
this.externalApiService = externalApiService;
}
@GetMapping("/call-api")
public String getFromExternalApi() {
return externalApiService.callExternalApi();
}
}
🧪 Example Output
When you access http://localhost:8080/call-api, it will make a call to the remote API (https://jsonplaceholder.typicode.com/posts/1) and return its JSON response.
✅ Best Practices
- Always handle exceptions (
RestClientException for RestTemplate, WebClientResponseException for WebClient).
- Consider adding timeouts and retry logic.
- Use
@HystrixCommand or Resilience4j for fault tolerance (optional).
- Use
WebClient for non-blocking applications; prefer RestTemplate for simpler sync apps.
Would you like an example with POST requests, headers, or authentication?