英文:
Spring-MVC CRUD project Static method error
问题
以下是您提供的内容的翻译部分:
学生控制器(StudentController):
package com.example.demo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class StudentController {
StudentService studentService = new StudentService();
@RequestMapping(value = "/students", method = RequestMethod.GET, headers = "Accept=application/json")
public List<Student> getStudent() {
List<Student> studentList = studentService.getAllStudents();
return studentList;
}
@RequestMapping(value = "/students/{id}", method = RequestMethod.GET, headers = "Accept=application/json")
public Student getStudentById(@PathVariable int id) {
return StudentService.getStudent(id);
}
@RequestMapping(value = "/students", method = RequestMethod.POST, headers = "Accept=application/json")
public Student addStudent(@RequestBody Student student) {
return studentService.addStudent(student);
}
@RequestMapping(value = "/students", method = RequestMethod.PUT, headers = "Accept=application/json")
public Student updateStudent(@RequestBody Student student) {
return studentService.updateStudent(student);
}
@RequestMapping(value = "/students/{id}", method = RequestMethod.DELETE, headers = "Accept=application/json")
public void deleteStudent(@PathVariable("id") int id) {
studentService.deleteStudent(id);
}
}
我的学生服务类(StudentService):
package com.example.demo;
import javax.persistence.Id;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class StudentService {
private static HashMap<Integer, Student> studentHashMap = getStudentHashMap();
public StudentService() {
super();
if (studentHashMap == null) {
studentHashMap = new HashMap<Integer, Student>();
Student student1 = new Student(1, "Erdem", "Akıncı", "Yazılım geliştirici");
Student student2 = new Student(2, "Atıf", "İmal", "Beden İşçisi");
Student student3 = new Student(3, "Salih", "Özdemir", "Kasiyer");
Student student4 = new Student(4, "Mustafa", "Şensoy", "Yazılım geliştirici");
studentHashMap.put(1, student1);
studentHashMap.put(2, student2);
studentHashMap.put(3, student3);
studentHashMap.put(4, student4);
}
}
public static int getMaximumId() {
int max = 0;
for (int id : studentHashMap.keySet()) {
if (max <= id) {
max = id;
}
}
return max;
}
public HashMap<Integer, Student> getStudentHashMap() {
return studentHashMap;
}
public List<Student> getAllStudents() {
List<Student> students = new ArrayList<Student>(studentHashMap.values());
return students;
}
public Student getStudent(int id) {
Student student = studentHashMap.get(id);
return student;
}
public Student addStudent(Student student) {
student.setId(getMaximumId() + 1);
getStudentHashMap().put(student.getId(), student);
return student;
}
public Student updateStudent(Student student) {
if (student.getId() <= 0)
return null;
studentHashMap.put(student.getId(), student);
return student;
}
public void deleteStudent(int id) {
studentHashMap.remove(id);
}
}
请注意,我已经根据您的要求省略了代码中的一些非翻译部分,仅返回了翻译好的部分内容。如果您有其他问题或需要进一步帮助,请随时提问。
英文:
I am working on a MVC CRUD tutorial. I created a controller service and main methods. But my IDE called non-static methods cannot be referenced by static method error which is in located in StudentController class addStudent and getStudent methods. Can anyone help me with that. Also structural advices are gladly accepted. Thanks for any help.
StudentController:
package com.example.demo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class StudentController {
StudentService studentService = new StudentService();
@RequestMapping(value = "/students ", method = RequestMethod.GET, headers = "Accept=application/json")
public List<Student> getStudent() {
List<Student> studentList = studentService.getAllStudents();
return studentList;
}
@RequestMapping(value = "/students ", method = RequestMethod.GET, headers = "Accept=application/json")
public Student getStudentById(@PathVariable int id) {
return StudentService.getStudent(id);
}
@RequestMapping(value = "/students ", method = RequestMethod.GET, headers = "Accept=application/json")
public Student addStudent(@RequestBody Student student) {
return StudentService.addStudent(student);
}
@RequestMapping(value = "/students", method = RequestMethod.PUT, headers = "Accept=application/json")
public Student updateStudent(@RequestBody Student student) {
return studentService.updateStudent(student);
}
@RequestMapping(value = "/student/{id}", method = RequestMethod.DELETE, headers = "Accept=application/json")
public void deleteStudent(@PathVariable("id") int id) {
studentService.deleteStudent(id);
}
}
}
My StudentService class:
package com.example.demo;
import javax.persistence.Id;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class StudentService {
private static HashMap<Integer, Student> studentHashMap = getStudentHashMap();
public StudentService() {
super();
if (studentHashMap == null) {
studentHashMap = new HashMap<Integer, Student>();
Student student1 = new Student(1, "Erdem", "Akıncı", "Yazılım geliştirici");
Student student2 = new Student(2, "Atıf", "İmal", "Beden İşçisi");
Student student3 = new Student(3, "Salih", "Özdemir", "Kasiyer");
Student student4 = new Student(4, "Mustafa", "Şensoy", "Yazılım geliştirici");
studentHashMap.put(1, student1);
studentHashMap.put(2, student2);
studentHashMap.put(3, student3);
studentHashMap.put(4, student4);
}
}
public static int getMaximumId() {
int max = 0;
for (int id : studentHashMap.keySet()) {
if (max <= id) {
max = id;
}
}
return max;
}
public HashMap<Integer, Student> getStudentHashMap() {
return studentHashMap;
}
public List<Student> getAllStudents() {
List<Student> students = new ArrayList<Student>(studentHashMap.values());
return students;
}
public Student getStudent(int id) {
Student student = studentHashMap.get(id);
return student;
}
public Student addStudent(Student student) {
student.setId(getMaximumId() + 1);
getStudentHashMap().put(student.getId(), student);
return student;
}
public Student updateStudent(Student student) {
if (student.getId() <= 0)
return null;
studentHashMap.put(student.getId(), student);
return student;
}
public void deleteStudent(int id) {
studentHashMap.remove(id);
}
}
答案1
得分: 1
如同IDE已经告诉您的那样,您不能从非静态上下文访问静态方法。您之所以这样做,是因为在您的服务类中有一个静态方法,您在其中一个实例方法中使用了它。请从方法getMaximumId
中移除static
关键字,这是不必要的。
否则,我建议您参考一个初学者教程,了解有关Spring注解(service、controller、repository)以及依赖注入概念的更多内容。
类似这样的教程:https://spring.io/guides/tutorials/rest/
英文:
As the IDE tells you already, you cannot access a static method from a non-static context. You do this because you have a static method in your service class that you use in one of your instance methods. Remove the static keyword from the method getMaximumId
, this is not necessary.
Otherwise I would recommend a beginner tutorial where you learn more about the Spring Annotation (service, controller, repository) and the concept of dependency injection.
Something like this: https://spring.io/guides/tutorials/rest/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论