gpt4 book ai didi

java - 在 Spring Boot 中创建路径

转载 作者:行者123 更新时间:2023-12-02 11:51:41 25 4
gpt4 key购买 nike

我有一个 Spring Boot 项目,我需要在其中创建与学生相关的 Spring Boot 应用程序。首先,我必须在 localhost:8080/students 上发送学生- 这对我来说效果很好第二,我必须按学生在本地主机上的索引发送:8080/students/index- 而且效果也很好第三,我必须通过本地主机上的学习程序发送学生:8080/students/studyProgram,并且存在问题学习程序是对象,所以我无法引用它,我尝试了一些不同的方法,所以我寻求帮助

My StudentDao,学生数据库所在的地方

    @Repository
public class StudentDao {

private static Map<String,Student>students;
private static Map<Integer,StudyProgram>programs;
static
{
programs = new HashMap<Integer, StudyProgram>()
{
{
put(1,new StudyProgram((long)41,"kni"));
put(2,new StudyProgram((long)42,"pet"));
put(3,new StudyProgram((long)43,"info"));
put(4,new StudyProgram((long)44,"iki"));
}
};
}

static
{
students = new HashMap<String, Student>()
{
{
put("141333", new Student("141333","asd","fgh",programs.get(1)));
put("14111", new Student("14111","Trajko","Trajkov",programs.get(2)));
put("140000", new Student("140000","Petko","Petkov",programs.get(3)));
put("145555", new Student("145555","Mile","Milev",programs.get(4)));
}
};
}

public Collection<Student> getAllStudents()
{
return this.students.values();
}

public Student getStudentByIndex(String index){
return this.students.get(index);
}
/* THIS HERE MAKES THE PROBLEM

public Student getStudentByStudyProgram(Student st){
return this.students.get(st.getStudyProgramId());
}
*/

}

我的学生服务

@Service
public class StudentService {
@Autowired
private StudentDao studentDao;
private StudyProgram studyProgram;
public Collection<Student> getAllStudents()
{
return this.studentDao.getAllStudents();
}

public Student getStudentByIndex(String index){
return this.studentDao.getStudentByIndex(index);
}

/* THIS HERE MAKES PROBLEM
public Student getStudentByStudyProgram(Student st){
return this.studentDao.getStudentByStudyProgram(st);
}

*/

}

我的学生 Controller

@RestController
@RequestMapping("/students")
public class StudentController {

@Autowired
private StudentService studentService;



@RequestMapping(method = RequestMethod.GET)
public Collection<Student>getAllStudents()
{
return studentService.getAllStudents();
}

//4.1 Get Students by index

@RequestMapping(value = "/{index}",method = RequestMethod.GET)
public Student getStudentByIndex(@PathVariable ("index") String index)
{
return this.studentService.getStudentByIndex(index);
}
/* AND THIS HERE ALSO
@RequestMapping(value = "/{studyProgram}",method = RequestMethod.GET)
public Student getStudentByStudyProgram(@PathVariable ("studyProgram") Student st)
{
return this.studentService.getStudentByStudyProgram(st);
}
*/

}

学生类

public class Student {

String index; //primary key
String name;
String lastname;
//ovde treba studyProgram na kraj
StudyProgram studyProgram;

public Student(String index, String name, String lastname, StudyProgram studyProgram)
{
this.index = index;
this.name = name;
this.lastname = lastname;
this.studyProgram = studyProgram;
}
Student(){}
WITH ALL METHODS IMPLEMENTED HERE

}

最后是我的 StudyProgram 类(class)

public class StudyProgram {
Long programId;
String name;

public StudyProgram(Long programId, String name)
{
this.programId = programId;
this.name = name;
}
StudyProgram(){}

}

最后我想问你能帮我在 localhost:8080/students/studyProgram 上制作一个 map 并通过他们的学习计划来映射它们

注释的字段中有需要修复的地方

谢谢

最佳答案

这是如何创建端点以通过学习计划 ID 检索您的学生

@RequestMapping(value = "/{studyProgramId}",method = RequestMethod.GET)
public Student getStudentByStudyProgram(@PathVariable ("studyProgramId") Long studyProgramId)
{
return this.studentService.getStudentByStudyProgramId(studyProgramId);
}

唯一需要做的就是实现 getStudentByStudyProgramId 方法,它应该可以工作。

关于java - 在 Spring Boot 中创建路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47839324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com