Spring-MVC CRUD项目静态方法错误

Spring-MVC CRUD project Static method error

我正在编写 MVC CRUD 教程。我创建了一个控制器服务和主要方法。但是我的 IDE 调用的非静态方法无法被位于 StudentController class addStudent 和 getStudent 方法中的静态方法错误引用。任何人都可以帮助我。也很乐意接受结构建议。感谢您的帮助。

学生控制器:

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);
    }


}



}

我的学生服务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);
    }

}

正如 IDE 已经告诉您的那样,您不能从 non-static 上下文访问静态方法。您这样做是因为您的服务 class 中有一个静态方法,您在其中一个实例方法中使用了该方法。从方法中删除静态关键字getMaximumId,这是没有必要的。

否则,我会推荐一个初学者教程,您可以在其中了解有关 Spring 注解(服务、控制器、存储库)和依赖项注入概念的更多信息。

像这样:https://spring.io/guides/tutorials/rest/