我必须将以下代码转换为非阻塞(异步)过程。
@RestController
@RequestMapping("/emp")
public class EmpController {
private EmployeeRepository empRepo;
@Autowired
public EmpController(EmployeeRepository empRepo)
{
this.empRepo=empRepo;
}
@PostMapping("/save/all")
@Consumes({MediaType.APPLICATION_JSON})
public void saveEmpAll(@RequestBody List<Employee> emps)
{
empRepo.saveEmpAll(emps);
}
}
@Repository
public class EmployeeRepository implements EmployeeDAO {
@Override
public void saveEmpAll(List<Employee> emps) {
Map<Long,Employee> employees=new HashMap<>();
emps.forEach(emp->{
emp.setDate(new Date());
employees.put(emp.getId(),emp);
});
hashOperations.putAll("EMP",employees);
}
}
在Spring Webflux中使用反应式编程
控制器
@PostMapping("/save/all")
@Consumes({MediaType.APPLICATION_JSON})
public void saveEmpAll(@RequestBody Flux<Employee> emps)
{
emps.collectMap(e->{
e.setDate(new Date());
// todo.............( can I do like this? is it the right way?)
});
}
存储库
@Override
public void saveEmpAll(Map<Long,Employee> employees) {
hashOperations.putAll("EMP",employees);
}
还有其他方法可以创建HashMap并将其与Flux以异步方式放入其中吗?