Use Case: Internal API Calls with Manual Clients

About

This use case demonstrates how one Spring Boot service (Service 1) can invoke a GET API exposed by another internal service (Service 2) using WebClient, a modern, non-blocking HTTP client introduced in Spring 5.

Service 2

Dependency

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

SampleController.java

package org.example.api;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SampleController {

    @GetMapping("/api/service-2/details")
    public ResponseEntity<String> getInterServiceDetails() {
        return ResponseEntity.ok("Service 2 processed successfully");
    }
}

application.yaml

Build and run the application

Service 1

Dependency

SampleController.java

WebClientConfig.java

SampleService.java

application.yaml

Build and run the application

Verification

Execute the service 1 API via Postman

Monitor the logs (have a look at the threads executing the code)

Handling error based on Http status

Sample Code to Create an employee, Fetch all employees and handling error based on Http status

Last updated