Use Case: Internal API Calls with Manual Clients
1. Service-1 calling Service-2 GET API
Dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
Sample Service 2

SampleController.java
package org.example.api;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RequiredArgsConstructor
@RestController
public class SampleController {
@GetMapping("/api/service-2/details")
public ResponseEntity<String> getInterServiceDetails() {
return ResponseEntity.ok("Service 2 processed successfully");
}
}
application.yaml
server:
port: 8082
Build and run the service 2 application.
Sample Service 1

ResttemplateConfig.java
package org.example.config;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import java.time.Duration;
@Configuration
public class ResttemplateConfig {
@Bean
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
return restTemplateBuilder
.setConnectTimeout(Duration.ofMillis(8000))
.setReadTimeout(Duration.ofMillis(8000))
.build();
}
}
SampleController.java
package org.example.api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@Slf4j
@RequiredArgsConstructor
@RestController
public class SampleController {
private final RestTemplate restTemplate;
private static final String SERVICE_2_ENDPOINT = "http://localhost:8082/api/service-2/details";
@GetMapping("/api/service-1/details")
public ResponseEntity<String> getInterServiceDetails() {
log.info("Calling service 2 API");
return restTemplate.getForEntity(SERVICE_2_ENDPOINT, String.class);
}
}
application.yaml
server:
port: 8081
Build and execute the application
Call the Service 1 API using Postman

Log Request & Response using Default httpclient
We can enable basic information logging like request, headers, response code etc by setting logging level below
logging.level.org.springframework.web.client.RestTemplate=DEBUG

We can enable full logging including request, response body, etc using Apache HttpClient
Log Request & Response using Apache httpclient
Add below dependency
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
Update RestTemplate config to use HttpComponentsClientHttpRequestFactory() request factory of Apache HTTPClient
package org.example.config;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ResttemplateConfig {
@Bean
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
RestTemplate restTemplate = new RestTemplate();
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
return restTemplate;
}
}
Add below logging property in application.yaml
logging.level.org.apache.http: DEBUG
Run both the service 1 and service 2 application from above example
Run the service 1 API from Postman and monitor the logs

Last updated