【问题标题】:How to get findAll() method to work on a Iterable object如何让 findAll() 方法在 Iterable 对象上工作
【发布时间】:2019-06-07 16:37:55
【问题描述】:

我试图在服务层中编写一个方法,该方法返回一个对象列表,以便我可以将它传递给我的 API 控制器。我的 findAll() 方法给了我错误:找到不兼容的类型:Iterable。必需:列表。所以我想使用 Set 而不是 List 但它给了我:无法推断参数(无法解析构造函数)。

我不知道我在这里做错了什么以及为什么我的学生对象被视为可迭代对象。任何帮助将不胜感激!

我的代码如下:

ServiceImpl

@Service
@RequiredArgsConstructor
public class StudentServiceImpl implements StudentService {

    @Autowired
    private final StudentRepository studentRepository;

    //Incompatible types found: Iterable. Required: List
    public List<Student> findAll() {
        return studentRepository.findAll();
    }

    //Cannot infer arguments (unable to resolve constructor)
    public Set<Student> getStudents()
    {
        Set<Student> students = new HashSet<>(studentRepository.findAll());
        return students;
    }

    public ArrayList<Student> getStudentsList(){
        return (ArrayList<Student>) this.studentRepository.findAll();
    }
}

服务

public interface StudentService {

    List<Student> findAll();
    Set<Student> getStudents();
    ArrayList<Student> getStudentsList()


}

API 控制器

@RestController
@RequestMapping("/api/v1/students")

public class StudentAPIController {

    private final StudentRepository studentRepository;

    public StudentAPIController(StudentRepository studentRepository) {
        this.studentRepository = studentRepository;
    }

    //cannot resolve .getStudentsList
    @GetMapping
    public ResponseEntity<List<Student>> findAll() {
        return ResponseEntity.ok(StudentServiceImpl.getStudentsList);
    }

    @GetMapping
    public ResponseEntity<List<Student>> findAll() {
        return ResponseEntity.ok(StudentServiceImpl.findAll());
    }
}

普通 StudentController

@Controller
@RequestMapping("/s")
public class StudentController {

    private final StudentRepository studentRepository;

    public StudentController(StudentRepository studentRepository){
        this.studentRepository = studentRepository;
    }

    @GetMapping
    public ModelAndView list(){
        Iterable<Student> students = this.studentRepository.findAll();
        return new ModelAndView("students/list" , "students", students);
    }

    @GetMapping("{id}")
    public ModelAndView view(@PathVariable("id") Student student) {
        return new ModelAndView("students/view", "student", student);
    }
}

StudentRepository

public interface StudentRepository extends CrudRepository<Student, Long> {

}

【问题讨论】:

  • 不明白你的问题,ypu 得到编译错误?什么是 StudentService 代码?在 JpaRepository 中: List findAll();是定义
  • 我已经添加了我的服务和存储库代码。我正在使用 CrudRepository 而不是 JpaRepository,所以也许我的问题可能出在那儿?

标签: java spring spring-boot spring-mvc


【解决方案1】:

在JPA或者Dao层这样使用

public List<T> findAll() {
            return entityManager.createQuery("your query").getResultList();
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-13
    • 2016-10-02
    • 2016-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-30
    相关资源
    最近更新 更多