如何在 Spring Boot 中返回响应但继续执行循环?

nar*_*007 4 java asynchronous spring-boot

因此,当我访问 enpoint(POST 请求)时,我首先检查数据库中是否已存在条目。如果是,我想将其返回给用户(用于跟踪目的)并继续操作。但我想将 id 返回给用户并继续此过程。如何实现这一目标?

@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

Run Code Online (Sandbox Code Playgroud)

小智 7

您可以在不同的线程中异步运行该进程,同时主线程返回 id 作为服务响应。

查看此博客,了解如何使用 spring @Async 注释定义异步操作 https://www.baeldung.com/spring-async