How to access another service from a Spring application?
sobes.tech AI
Answer from AI
To call another service from a Spring application, you can use the following approaches:
-
HttpClient (Java Standard Library): Basic method for sending HTTP requests. Requires manual handling of connections and response parsing.
import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.io.IOException; public class HttpClientExample { public static void main(String[] args) throws IOException, InterruptedException { HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("http://other-service/api/data")) .GET() // Or .POST(HttpRequest.BodyPublishers.ofString("...")) for POST .build(); HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); System.out.println(response.statusCode()); System.out.println(response.body()); } } -
RestTemplate (Spring Framework): Simplifies executing HTTP requests, supports object-to-JSON/XML conversion and vice versa. Considered deprecated in favor of
WebClient.import org.springframework.web.client.RestTemplate; public class RestTemplateExample { public static void main(String[] args) { RestTemplate restTemplate = new RestTemplate(); String result = restTemplate.getForObject("http://other-service/api/data", String.class); System.out.println(result); } } -
WebClient (Spring WebFlux): Non-blocking (reactive) HTTP client, preferred in modern Spring applications. Provides better scalability.
import org.springframework.web.reactive.function.client.WebClient; public class WebClientExample { public static void main(String[] args) { WebClient webClient = WebClient.create("http://other-service"); webClient.get() .uri("/api/data") .retrieve() .bodyToMono(String.class) .subscribe(System.out::println); // Non-blocking call } } -
Feign Client (Spring Cloud OpenFeign): Declarative REST client. Allows defining an interface, and Feign generates its implementation for executing HTTP requests. Used in microservice architecture with Service Discovery.
Dependency:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>@EnableFeignClientsannotation on the main application class.Client interface:
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; @FeignClient(name = "other-service") // Service name in Service Discovery public interface OtherServiceClient { @GetMapping("/api/data") String getData(); }Usage by a service:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class MyService { @Autowired private OtherServiceClient otherServiceClient; public String callOtherService() { return otherServiceClient.getData(); } }
The choice of approach depends on the application's needs:
HttpClient: for simple, basic integrations.RestTemplate: for simpler Spring MVC applications requiring synchronous calls.WebClient: for reactive applications where performance and non-blocking behavior are important.Feign Client: for microservice architectures with Service Discovery, requiring a declarative approach and abstraction of HTTP calls.