具有CompletableFuture的其余端点与没有CompletableFuture的其余端点

时间:2019-10-16 21:15:27

标签: java spring rest spring-mvc completable-future

PersonController中的getPerson端点和PersonController1中的以下代码有什么区别?

在这种情况下,使用CompletableFuture有什么好处?

@RestController
public class PersonController {

    @Autowired
    private PersonService personService;


    @GetMapping("/persons/{personId}" )
    public CompletableFuture<Person> getPerson(@PathVariable("personId") Integer personId) {

        return CompletableFuture.supplyAsync(() -> personService.getPerson(personId));
    }
}
@RestController
public class PersonController1 {

    @Autowired
    private PersonService personService;


    @GetMapping("/persons/{personId}" )
    public Person getPerson(@PathVariable("personId") Integer personId) {

        return personService.getPerson(personId);
    }
}
@Service
public class PersonService {


    @Autowired
    PersonRepository personRepository;

    @Transactional
    public Person getPerson(int personId) {

        return personRepository.getPerson(personId);//this will get the data from the DB

    }
}

0 个答案:

没有答案