Sobes.tech
Junior — Middle

Is it possible to send the same request multiple times within a single controller call?

sobes.tech AI

Answer from AI

Within a single controller call (for example, in Spring MVC), it is technically possible to send the same request multiple times if 'request' refers to, for example, an HTTP request to another service or a method call. However, this is not considered good practice, as it can lead to excessive load, data duplication, and performance degradation.

If it concerns reprocessing the same incoming HTTP request, it is usually not done — the controller is called once for each incoming request. If you need to repeat some operation, it is better to extract it into a separate method and call it multiple times.

An example of multiple calls to the same external API request within a controller:

@RestController
public class MyController {
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/call")
    public String callMultipleTimes() {
        String url = "http://external-service/api/data";
        String result1 = restTemplate.getForObject(url, String.class);
        String result2 = restTemplate.getForObject(url, String.class);
        return result1 + ", " + result2;
    }
}

But it is better to avoid such repeated calls unless necessary.

Is it possible to send the same request multiple… - sobes.tech