Spring / Spring Boot 4 Basics Interview Questions
What are HTTP Service Clients in Spring Boot 4 and how do you define them?
HTTP Service Clients (also called declarative HTTP clients) allow you to define REST client interfaces using annotations, similar to Feign clients but built natively into Spring. Spring Boot 4 adds auto-configuration for @HttpExchange interfaces, making it easy to create and inject HTTP clients as Spring beans.
// Define a declarative HTTP client interface: public interface ProductClient { @GetExchange("/api/products/{id}") ProductDto getProduct(@PathVariable String id); @GetExchange("/api/products") List<ProductDto> getAllProducts( @RequestParam int page, @RequestParam int size ); @PostExchange("/api/products") ProductDto createProduct(@RequestBody CreateProductRequest request); @DeleteExchange("/api/products/{id}") void deleteProduct(@PathVariable String id); } // Spring Boot 4: auto-configuration via spring.http.clients.* # application.properties: spring.http.clients.product.base-url=https://products-api.example.com spring.http.clients.product.connect-timeout=5s spring.http.clients.product.read-timeout=10s // Use it like any other Spring bean: @Service @RequiredArgsConstructor public class OrderService { private final ProductClient productClient; // injected automatically public Order createOrder(String productId, int qty) { ProductDto product = productClient.getProduct(productId); // ... return new Order(product, qty); } } // Manual configuration (without auto-config): @Configuration public class ClientConfig { @Bean public ProductClient productClient(RestClient.Builder builder) { return HttpServiceProxyFactory .builderFor(RestClientAdapter.create( builder.baseUrl("https://products-api.example.com").build() )) .build() .createClient(ProductClient.class); } }
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
