在Spring Boot中创建路径

时间:2017-12-15 20:17:36

标签: java spring spring-mvc spring-boot

我有春季启动项目,我需要创建与学生相关的Spring启动应用程序。 在第一,我必须在本地主机上发送学生:8080 /学生 - 它适合我 在第二,我必须通过他们的localhost:8080 / students / index索引发送学生 - 而且效果也很好 在第三,我必须通过他们的学习计划发送学生本地主机:8080 /学生/学习计划和问题研究程序是OBJECT所以我不能参考它,我尝试了一些不同的方式,所以我要求帮助

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

$sponsorEm = $em->getRepository(TicketDAO::class);
$spo = $sponsorEm->find("2");
var_dump($spo);

我的学生服务

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

    */

}

学生班

@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);
    }
    */

}

最后我的StudyProgram课程

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

}

所以最后我想问你能帮我画一张地图在localhost:8080 / students / studyProgram并按他们的学习计划映射

在评论的字段中,需要修复

谢谢

1 个答案:

答案 0 :(得分:1)

这是,如何创建端点以按学习计划ID

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

唯一需要做的是,实施 getStudentByStudyProgramId 方法,它应该可行。