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

huangapple go评论77阅读模式
英文:

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 = &quot;/students &quot;, method = RequestMethod.GET, headers = &quot;Accept=application/json&quot;)
public List&lt;Student&gt; getStudent() {
List&lt;Student&gt; studentList = studentService.getAllStudents();
return studentList;
}
@RequestMapping(value = &quot;/students &quot;, method = RequestMethod.GET, headers = &quot;Accept=application/json&quot;)
public Student getStudentById(@PathVariable int id) {
return StudentService.getStudent(id);
}
@RequestMapping(value = &quot;/students &quot;, method = RequestMethod.GET, headers = &quot;Accept=application/json&quot;)
public Student addStudent(@RequestBody Student student) {
return StudentService.addStudent(student);
}
@RequestMapping(value = &quot;/students&quot;, method = RequestMethod.PUT, headers = &quot;Accept=application/json&quot;)
public Student updateStudent(@RequestBody Student student) {
return studentService.updateStudent(student);
}
@RequestMapping(value = &quot;/student/{id}&quot;, method = RequestMethod.DELETE, headers = &quot;Accept=application/json&quot;)
public void deleteStudent(@PathVariable(&quot;id&quot;) 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&lt;Integer, Student&gt; studentHashMap = getStudentHashMap();
public StudentService() {
super();
if (studentHashMap == null) {
studentHashMap = new HashMap&lt;Integer, Student&gt;();
Student student1 = new Student(1, &quot;Erdem&quot;, &quot;Akıncı&quot;, &quot;Yazılım geliştirici&quot;);
Student student2 = new Student(2, &quot;Atıf&quot;, &quot;İmal&quot;, &quot;Beden İş&#231;isi&quot;);
Student student3 = new Student(3, &quot;Salih&quot;, &quot;&#214;zdemir&quot;, &quot;Kasiyer&quot;);
Student student4 = new Student(4, &quot;Mustafa&quot;, &quot;Şensoy&quot;, &quot;Yazılım geliştirici&quot;);
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 &lt;= id) {
max = id;
}
}
return max;
}
public HashMap&lt;Integer, Student&gt; getStudentHashMap() {
return studentHashMap;
}
public List&lt;Student&gt; getAllStudents() {
List&lt;Student&gt; students = new ArrayList&lt;Student&gt;(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() &lt;= 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/

huangapple
  • 本文由 发表于 2020年9月21日 22:40:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63994562.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定