当前位置: 首页 > 知识库问答 >
问题:

GET REQUEST无法正确返回教师数据导致500个内部服务器错误

米树
2023-03-14

我想让邮递员返回这个:显示通过id搜索的教员,并在教员内部显示学生的科目、教授和学生的成绩数据。

{
    "id": 1,
    "university_id": 1,
    "name": "FSHMN",
    "Enrolled Students:": [
        {
           
        {
            "id": "5",
            "username": "Student5",
            "password": "d123",
            "fullname": "Student",
            "email": "Student@gmail.com",
            "subjects": [
                {
                    "id": 1,
                    "name": "Programim 1",
                    "Professor": [
                        {
                            "id": 1,
                            "first name": "Stephen",
                            "last name": "Hawking",
                            "Title": "Docen"
                        }
                    ],
                    "Grade": [
                        {
                            "grade_id": 2,
                            "mark": 9,
                            "description": "Very Good"
                        }
                    ]
                },
                {
                    "id": 2,
                    "name": "Programim 2",
                    "Professor": [
                        {
                            "id": 2,
                            "first name": "John",
                            "last name": "VonNeuman",
                            "Title": "Inordinar"
                        }
                    ],
                    "Grade": [
                        {
                            "grade_id":1,
                            "mark": 10,
                            "description": "Very well Done"
                        }
                    ]
                },
                {
                    "id": 3,
                    "name": "Calculus",
                    "Professor": [
                        {
                            "id": 3,
                            "first name": "Albert",
                            "last name": "Einstein",
                            "Title": "Ordinar"
                        }
                    ],
                    "Grade": [
                        {
                            "grade_id": 4,
                            "mark": 7,
                            "description": "well"
                        }
                    ]
                },
                {
                    "id": 4,
                    "name": "Discrete mathematics",
                    "Professor": [
                        {
                            "id": 4,
                            "first name": "John",
                            "last name": "Feynman",
                            "Title": "Rektor"
                        }
                    ],
                    "Grade": [
                        {
                            "grade_id": 2,
                            "mark": 8,
                            "description": " Good"
                        }
                    ]
                }
            ]
        }
    ]
}

下面是GET请求调用的方法:

public Faculty getFacultyStudent(int id)引发异常{连接=null;

    Faculty faculty = new Faculty();
    Student student = new Student();

    HashMap<String,List<Student>> studentFacultyMap=new HashMap<>();
    HashMap<String,Professor> professorHashMap=new HashMap<>();
    HashMap<String, List<StudentMark>> studentMarksHashMap=new HashMap<>();

    faculty.setStudentList(new ArrayList<>());

    
    Map<String,Subject> subjectMap = new HashMap<>();


    try {
        connection = new MysqlDbConnectionService().getConnection();


        String select = "SELECT f.fid, f.fname, f.university_id,  s.user_id,  s.username,  s.password,  s.fullname,  s.email, "+
                " s.email, subj.id , subj.name , p.professor_id, p.first_name, p.last_name, p.title, g.grade_id, g.mark, g.description" +
                " FROM faculty f " +
                "         INNER JOIN student_faculty sf ON sf.faculty_id=f.fid " +
                "         INNER JOIN student s ON sf.student_id=s.user_id " +
                "         INNER JOIN faculty_subject fs ON f.fid = fs.faculty_id " +
                "         INNER JOIN subject subj ON fs.subject_id = subj.id " +
                "         INNER JOIN professor_subject ps ON ps.subject_id = subj.id " +
                "         INNER JOIN professor p ON ps.prof_id = p.professor_id " +
                "         INNER JOIN student_subject_marks sm ON sm.student_id = s.user_id and sm.subject_id = subj.id" +
                "         INNER JOIN grade g ON sm.grade_id = g.grade_id " +
                "WHERE fid = ?";


        PreparedStatement ps = connection.prepareStatement(select);

        ps.setInt(1, id);

        ResultSet rs = ps.executeQuery();

        studentFacultyMap= facultyService.getFacultyStudentMap(id);
        professorHashMap=facultyService.getProfessors(id);

        studentMarksHashMap=facultyService.getStudentSubjectMarks(id);


        while (rs.next()) {

            if(faculty.getFid()==0) {
                faculty.setFid(rs.getInt("fid"));
                faculty.setUniversityid(rs.getInt("university_id"));
                faculty.setFname(rs.getString("fname"));

            }

          
            String subjectID=rs.getString("id");


            Subject subject =null;


            if(!subjectMap.containsKey(subjectID)) {
                subject = new Subject();

                subject.setProfessorList(new ArrayList<>());
                subject.setMarkList(new ArrayList<>());
                subject.setId(rs.getInt("id"));
                subject.setName(rs.getString("name"));

                subjectMap.put(subjectID, subject);

                subject.getProfessorList().addAll(professorHashMap.values());
           subject.getMarkList().addAll((Collection<? extends StudentMark>); studentMarksHashMap.values());


            }
            else{
                subject = subjectMap.get(subjectID);
            }


            student= (Student) studentFacultyMap.values();
            student.getSubjectList().add(subject);

        }

    } catch (Exception e) {
        System.out.println(e + " Retrieve not successful");

    }


    faculty.getStudentList().add(student);


    return faculty;

}

此方法调用另外3个方法来生成学生、教授和成绩数据:此方法获取学生:

   private HashMap<String,List<Student>> getFacultyStudentMap(int id){
        Connection connection = null;

        HashMap<String,List<Student>> studentMap=new HashMap<>();
        try {
            connection = new MysqlDbConnectionService().getConnection();
            String select = "SELECT f.fid, f.fname, f.university_id,  s.user_id,  s.username,  s.password,  s.fullname,  s.email, " +
                    " s.email, subj.id , subj.name , p.professor_id, p.first_name, p.last_name, p.title, g.grade_id, g.mark, g.description" +
                    " FROM faculty f " +
                    "         INNER JOIN student_faculty sf ON sf.faculty_id=f.fid " +
                    "         INNER JOIN student s ON sf.student_id=s.user_id " +
                    "         INNER JOIN faculty_subject fs ON f.fid = fs.faculty_id " +
                    "         INNER JOIN subject subj ON fs.subject_id = subj.id " +
                    "         INNER JOIN professor_subject ps ON ps.subject_id = subj.id " +
                    "         INNER JOIN professor p ON ps.prof_id = p.professor_id " +
                    "         INNER JOIN student_subject_marks sm ON sm.student_id = s.user_id and sm.subject_id = subj.id" +
                    "         INNER JOIN grade g ON sm.grade_id = g.grade_id " +
                    "WHERE fid = ?";
//
//
            PreparedStatement ps = connection.prepareStatement(select);

            ps.setInt(1, id);
//
            ResultSet rs = ps.executeQuery();

            String userID = rs.getString("user_id");
            Student student = null;

            while (rs.next()) {
                if (!studentMap.containsKey(userID)) {
                    student = new Student();

                    student.setSubjectList(new ArrayList<>());
                    student.setId(rs.getString("user_id"));
                    student.setUsername(rs.getString("username"));
                    student.setPassword(rs.getString("password"));
                    student.setFullName(rs.getString("fullname"));
                    student.setEmail(rs.getString("email"));

                   studentMap.put(userID, (List<Student>) student);
                }
                else{
                    student = (Student) studentMap.get(userID);

                }

            }

        }
        catch(Exception e)
        {
            System.out.println("FacultyStudentMap: " + e);
        }
        return studentMap;


    }

此方法可获得教授:

private HashMap<String,Professor> getProfessors(int id){
        Connection connection = null;
        HashMap<String,Professor> professorMap=new HashMap<>();

        try{
            connection = new MysqlDbConnectionService().getConnection();

            String select = "SELECT f.fid, f.fname, f.university_id,  s.user_id,  s.username,  s.password,  s.fullname,  s.email, "+
                    " s.email, subj.id , subj.name , p.professor_id, p.first_name, p.last_name, p.title, g.grade_id, g.mark, g.description" +
                    " FROM faculty f " +
                    "         INNER JOIN student_faculty sf ON sf.faculty_id=f.fid " +
                    "         INNER JOIN student s ON sf.student_id=s.user_id " +
                    "         INNER JOIN faculty_subject fs ON f.fid = fs.faculty_id " +
                    "         INNER JOIN subject subj ON fs.subject_id = subj.id " +
                    "         INNER JOIN professor_subject ps ON ps.subject_id = subj.id " +
                    "         INNER JOIN professor p ON ps.prof_id = p.professor_id " +
                    "         INNER JOIN student_subject_marks sm ON sm.student_id = s.user_id and sm.subject_id = subj.id" +
                    "         INNER JOIN grade g ON sm.grade_id = g.grade_id " +
                    "WHERE fid = ?";

            PreparedStatement ps = connection.prepareStatement(select);

            ps.setInt(1, id);
//
            ResultSet rs = ps.executeQuery();

            String professorID = rs.getString("professor_id");
            Professor professor = null;

            while(rs.next())
            {
                if (!professorMap.containsKey(professorID)) {
                    professor = new Professor();

                    professor.setProfessor_id(rs.getInt("professor_id"));
                    professor.setFirst_name(rs.getString("first_name"));
                    professor.setLast_name(rs.getString("last_name"));
                    professor.setTitle(rs.getString("title"));

                   professorMap.put(professorID, professor);
                }
                else{
                    professor = (Professor) professorMap.get(professorID);
                }

            }

        }
        catch(Exception e)
        {
            System.out.println("GetProfessor: " + e);
        }

        return professorMap;
    }

此方法获取等级:

 private HashMap<String,List<StudentMark>> getStudentSubjectMarks(int id){
        Connection connection = null;

        HashMap<String,List<StudentMark>> studentMark=new HashMap<>();

        try{
            connection = new MysqlDbConnectionService().getConnection();

            String select = "SELECT f.fid, f.fname, f.university_id,  s.user_id,  s.username,  s.password,  s.fullname,  s.email, "+
                    " s.email, subj.id , subj.name , p.professor_id, p.first_name, p.last_name, p.title, g.grade_id, g.mark, g.description" +
                    " FROM faculty f " +
                    "         INNER JOIN student_faculty sf ON sf.faculty_id=f.fid " +
                    "         INNER JOIN student s ON sf.student_id=s.user_id " +
                    "         INNER JOIN faculty_subject fs ON f.fid = fs.faculty_id " +
                    "         INNER JOIN subject subj ON fs.subject_id = subj.id " +
                    "         INNER JOIN professor_subject ps ON ps.subject_id = subj.id " +
                    "         INNER JOIN professor p ON ps.prof_id = p.professor_id " +
                    "         INNER JOIN student_subject_marks sm ON sm.student_id = s.user_id and sm.subject_id = subj.id" +
                    "         INNER JOIN grade g ON sm.grade_id = g.grade_id " +
                    "WHERE fid = ?";

            PreparedStatement ps = connection.prepareStatement(select);

            ps.setInt(1, id);
//
            ResultSet rs = ps.executeQuery();

            String markID = rs.getString("grade_id");
            StudentMark mark = null;

            while(rs.next())
            {
                if (!studentMark.containsKey(markID)) {
                    mark = new StudentMark();
                    mark.setGrade_id(rs.getInt("grade_id"));
                    mark.setMark(rs.getInt("mark"));
                    mark.setDescription(rs.getString("description"));

                    studentMark.put(markID, (List<StudentMark>) mark);
                }

                else{
                    mark = (StudentMark) studentMark.get(markID);
                }


            }

        }
        catch(Exception e)
        {
            System.out.println("getStudentSubjectMark: " + e);
        }

        return studentMark;
    }

这是主题和学生班:

学生班级:

package com.common.db.domain;


import com.google.gson.annotations.SerializedName;

import java.util.List;

public class Student {


    @SerializedName("id")
    private String id;

    @SerializedName("username")
    private String username;

    @SerializedName("password")
    private String password;

    @SerializedName("fullname")
    private String fullName;

    @SerializedName("email")
    private String email;

    @SerializedName("subjects")
    private List<Subject> subjectList;


    public Student() {

    }


    public Student(String id, String username, String password, String fullName, String email) {
        super();
        this.id = id;
        this.username = username;
        this.password = password;
        this.fullName = fullName;
        this.email = email;
    }


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public List<Subject> getSubjectList() {
        return subjectList;
    }

    public void setSubjectList(List<Subject> subjectList) {
        this.subjectList = subjectList;
    }
}

主题类别:

package com.common.db.domain;
import com.google.gson.annotations.SerializedName;

import java.util.List;

public class Subject {




    @SerializedName("id")
    private int id;

    @SerializedName("name")
    private String name;

    @SerializedName("Professor")
     private List<Professor> professorList;

    @SerializedName("Grade")
    private List<StudentMark> markList;


    public Subject() {
        this.id = id;
        this.name=name;
    }

    public  void setId(int id)
    {
      this.id=id;
    }

    public int getId()
    {
        return id;
    }

    public void setName(String name)
    {
        this.name=name;
    }

    public String getName()
    {
        return name;
    }


    public List<Professor> getProfessorList() {
        return professorList;
    }

    public void setProfessorList(List<Professor> professorList) {
        this.professorList = professorList;
    }

    public List<StudentMark> getMarkList() {
        return markList;
    }

    public void setMarkList(List<StudentMark> markList) {
        this.markList = markList;
    }
}

我知道我没有正确定义这些方法,也没有正确地将这3个方法与主方法互连,因此我需要在代码中对这些方法做哪些更改才能正确返回教员数据。

共有1个答案

贺恩
2023-03-14

以下是我的问题的解决方案:

我对这些方法进行了重大更改:

这是GET请求调用的主要方法:

 public List<Faculty> getFacultiesIncludingSubObjects() throws Exception {
        Connection connection = null;

        List<Faculty> faculties=getFaculty();
        HashMap<String, List<Subject>> studentSubjectMap = new HashMap<>();
        HashMap<String, List<StudentMark>> studentSubjectGradeMap = new HashMap<>();
        HashMap<String, List<Student>> facultyStudentMap = new HashMap<>();
        try {
            connection = new MysqlDbConnectionService().getConnection();
            studentSubjectMap=getStudentSubjectMap(connection);
            studentSubjectGradeMap=getStudentSubjectGradeMap(connection);
            facultyStudentMap=getFacultyStudentMap(connection);
        }finally {
            connection.close();
        }
        for(Faculty faculty:faculties){
            faculty.setStudentList(facultyStudentMap.get(faculty.getFid()+""));
            if(faculty.getStudentList()!=null){
                for(Student student:faculty.getStudentList()){
                    student.setSubjectList(studentSubjectMap.get(student.getId()));
                    if(student.getSubjectList()!=null){
                        for(Subject sb:student.getSubjectList()){
                            sb.setMarkList(studentSubjectGradeMap.get(student.getId()+"_"+sb.getId()));
                        }
                    }
                }
            }
        }
        return faculties;
    }

这一行:列表

调用getFaculty方法:

public ArrayList<Faculty> getFaculty() throws Exception {

        ArrayList<Faculty> data = new ArrayList<Faculty>();
        Connection connection = null;
        try {
            connection = new MysqlDbConnectionService().getConnection();

            String select = "select * from faculty";
            PreparedStatement ps = connection.prepareStatement(select);
            ResultSet rs = ps.executeQuery();

            Faculty model = null;

            while (rs.next()) {
                model = new Faculty();
                model.setFid(rs.getInt("fid"));
                model.setUniversityid(rs.getInt("university_id"));
                model.setFname(rs.getString("fname"));
                data.add(model);
            }
        } catch (Exception e) {
            System.out.println(e + "Retrieve not successful");
        }
        return data;
    }

在main方法内部还调用了3个其他方法:

主题方法:

 public HashMap<String, List<Subject>> getStudentSubjectMap(Connection connection) throws SQLException {
        HashMap<String, List<Subject>> result=new HashMap<>();
        PreparedStatement ps = connection.prepareStatement("select s.user_id as student_id,sb.*\n" +
                "from student s\n" +
                "         inner join student_faculty sf on s.user_id = sf.student_id\n" +
                "         inner join faculty f on sf.faculty_id = f.fid\n" +
                "         inner join faculty_subject fs on f.fid = fs.faculty_id\n" +
                "         inner join subject sb on fs.subject_id = sb.id;");
        ResultSet rs=ps.executeQuery();
        while(rs.next()){
            String studentId=rs.getString("student_id");
            result.putIfAbsent(studentId,new ArrayList<>());
            Subject subject=new Subject();
            subject.setId(rs.getInt("sb.id"));
            subject.setName(rs.getString("sb.name"));
            result.get(studentId).add(subject);
        }
        return result;
    }

学生方法:

 public HashMap<String, List<Student>> getFacultyStudentMap(Connection connection) throws SQLException {
        HashMap<String, List<Student>> result=new HashMap<>();
        PreparedStatement ps = connection.prepareStatement("select sf.faculty_id, s.*\n" +
                "from student s\n" +
                "         inner join student_faculty sf on sf.student_id = s.user_id;");
        ResultSet rs=ps.executeQuery();
        while(rs.next()){
            String facultyId=rs.getString("faculty_id");
            result.putIfAbsent(facultyId,new ArrayList<>());
            Student student=new Student();
            student.setId(rs.getString("user_id"));
            student.setUsername(rs.getString("username"));
            student.setPassword(rs.getString("password"));
            student.setFullName(rs.getString("fullname"));
            student.setEmail(rs.getString("email"));
            result.get(facultyId).add(student);
        }
        return result;
    }

评分方法:

public HashMap<String, List<StudentMark>> getStudentSubjectGradeMap(Connection connection) throws SQLException {
        HashMap<String, List<StudentMark>> result=new HashMap<>();
        PreparedStatement ps = connection.prepareStatement("select concat_ws('_', s.user_id, sb.id) as student_subject, sb.name, g.*\n" +
                "from student s\n" +
                "         inner join student_faculty sf on s.user_id = sf.student_id\n" +
                "         inner join faculty f on sf.faculty_id = f.fid\n" +
                "         inner join faculty_subject fs on f.fid = fs.faculty_id\n" +
                "         inner join subject sb on fs.subject_id = sb.id\n" +
                "         inner join student_subject_marks ssm on sb.id = ssm.subject_id and ssm.student_id = s.user_id\n" +
                "         inner join grade g on ssm.grade_id = g.grade_id;");
        ResultSet rs=ps.executeQuery();
        while(rs.next()){
            String studentSubjectId=rs.getString("student_subject");
            result.putIfAbsent(studentSubjectId,new ArrayList<>());
            StudentMark mark=new StudentMark();
            mark.setMark(rs.getInt("mark"));
            mark.setDescription(rs.getString("description"));
            result.get(studentSubjectId).add(mark);
        }
        return result;
    }

调用main方法的Jersey方法是:

@GET
public Response getFaculty() throws Exception {
    return Response.ok(new Gson().toJson(facultyService.getFacultiesIncludingSubObjects())).build();
}

 类似资料:
  • 问题内容: 我正在尝试执行此AJAX帖子,但是由于某些原因,我遇到了服务器500错误。我可以看到它在控制器中达到了断点。因此问题似乎出在回调上。任何人? 这是应该返回的字符串: 问题答案: 我怀疑服务器方法在通过断点后会引发异常。使用Firefox / Firebug或IE8开发人员工具查看您从服务器获得的实际响应。如果有异常,您将获得YSOD html,这应有助于您确定要查找的位置。 还有一件事

  • 在尝试使用Twython进行简单的推特搜索时 我得到了这个错误: 这里可能有什么问题?

  • 我在尝试进行返回JSON的ajax POST调用时出现内部错误。ajax调用来自JS页面: 这是我的Spring MVC调用方法: 问题是,我在这个类中只设置了几个字段(30个字段中有17个)。这是一个具有简单setter-getter的bean类。如果我设置了所有30个字段,效果很好。你能建议如何设置JSON响应,以便我可以纠正这个问题吗。

  • 我有一个由开发团队开发的社交网络项目。我正试图通过Xampp在我的本地主机上运行它,并已将网站升级到显示一些静态页面的基本html版本。此时,我收到许多内部服务器错误,我想问一下这背后的原因是否是我的服务器配置不正确。我没有使用服务器的经验,花了相当多的时间点击下面的文档。注意到许多“访问被拒绝”的错误,我假设这是服务器配置。我也一直在搜索我的apache文件来找出它,但没有成功。 [2017年7

  • 正如标题所说,我在使用Web API的post方法时遇到了500内部服务器错误。Get 方法工作正常,只是在 POST 中出现错误。 我使用fidler发送post请求: 响应标头:HTTP/1.1 500内部服务器错误 请求头:User-Agent:Fiddler主机:localhost:45379Content-Type:Application/jsonContent-Llong:41 Con

  • 我的浏览器控制台上有一个错误:“放置http://localhost:8000/post/2 500(内部服务器错误)” 我使用资源控制器和我的路由-路由e::资源('post','PostController'); 此处为更新帖子的资源控制器代码: 这里我的视图代码: Ajax代码: 顺便说一下,我在文件头上使用了元令牌{{csrf_token()}}。但是我在本地主机上发现了500个内部服务器