我想编写一个FindAll()方法,该方法返回所有Student对象的列表。但是CRUDRepository仅具有Iterable findAll()。目的是让所有学生进入列表,然后将其传递给API控制器,这样我就可以为所有学生提供http GET。将此方法转换为List FindAll()的最佳方法是什么?在我当前的代码中,StudentService中的findAll方法为我提供了发现的不兼容类型:可迭代。必需:列表错误。服务@Service@RequiredArgsConstructorpublic class StudentServiceImpl implements StudentService { @Autowired private final StudentRepository studentRepository; //Incompatible types found: Iterable. Required: List public List<Student> findAll() { return studentRepository.findAll(); }}API控制器@RestController@RequestMapping("/api/v1/students")public class StudentAPIController { private final StudentRepository studentRepository; public StudentAPIController(StudentRepository studentRepository) { this.studentRepository = studentRepository; } @GetMapping public ResponseEntity<List<Student>> findAll() { return ResponseEntity.ok(StudentServiceImpl.findAll()); }}学生资料库public interface StudentRepository extends CrudRepository<Student, Long> {} (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 如果使StudentRepository从JpaRepository继承,则可以通过返回List获得findAll()方法。public interface StudentRepository extends JpaRepository<Student, Long> {}参考资料:https://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/repository/JpaRepository.html#findAll-- (adsbygoogle = window.adsbygoogle || []).push({});
09-12 02:25