【发布时间】:2021-12-30 09:01:49
【问题描述】:
如何在不传递任何变量的情况下在 Spring Boot 中检索数据列表。我们只传递 GET 请求的 URL。
例如,在下面的代码中,我传递了“roll no”,它工作正常,它正在获取相应的学生详细信息。
@GetMapping{"/fetch/{rollNo}")
public List<StudentDetail> findStudentDeatilbyRollNo (@PathVariable String rollNo){
return StudentService.findStudentDetailByRollNo(rollNo);
}
但是当我想在不传递参数的情况下获取所有学生数据时,它给了我错误“无法从静态上下文引用非静态方法 'fetchAllStudentDetail()'”
@GetMapping{"/allStudentDetails")
public List<StudentDetail> fetchAllStudentDeatil(){
return StudentService.fetchAllStudentDeatil();
}
有人,请帮我解决这个问题。
【问题讨论】:
-
看来
findStudentDetailByRollNo是static,而fetchAllStudentDeatil不是。 -
@Arnaud 是的,但是如果我想在不传递任何变量的情况下获取学生详细信息,解决方案是什么?
-
把两者都设为静态?
-
@Christopher 怎么做?
-
只需将
static关键字添加到方法的签名中,例如public static List<Student> fetchAllStudentDeatil。此外,当您在这里时,您可能希望修正方法名称中的拼写错误:)。
标签: java spring spring-boot spring-mvc