Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return a response but continue to execute a loop in spring boot?

So when I visit an enpoint(POST request), I first check if an entry already exists in my database. If yes, I want to return it to the user (for tracking purposes) and continue an operation. But I would like to return the id to the user and continue this process. How to achieve this??

@RestController
public class StudentController {
    @Autowired
    private StudentService service;

    @PostMapping("/storeInDB")
    @ResponseBody
    public File_Tracking saveStudentDetails(@RequestBody Student student ) throws IOException {

        List<Students> student = new ArrayList<>();


        int continue = 0;
        int id = 0;


            id = service.getId(); // I want to return this and continue some other process which follows

like image 308
nar-007 Avatar asked Sep 08 '25 00:09

nar-007


1 Answers

You can run the process asychronously in a different thread ,while your main thread returns the id as the service response.

Check this blog about how to define Async operations using spring @Async annotation https://www.baeldung.com/spring-async

like image 120
Sandeep Lakdawala Avatar answered Sep 09 '25 15:09

Sandeep Lakdawala