Home > Net >  Submitted a post request in postman and only 4 entities mapped(including the primary key)
Submitted a post request in postman and only 4 entities mapped(including the primary key)

Time:10-10

I recently tried sending a post request via postman and not everything mapped correctly. Despite the fact that I had values set for each attribute within the json file it only mapped values to 4 attributes. The other attributes were marked as null. It also did something weird where it displayed the values for the other entities in my database. I am assuming the issue is some way linked to this. Below I have a screenshot of before and the response I got back when I submitted the request. I also added the model,service and controller classes as well as the xml file I converted to JSON for the Student Entity. Any help at all I would appreciate greatly.

Before

Response

Student Model Class

@Entity
@Table(name = "student")
@XmlRootElement(name = "student")
@XmlAccessorType(XmlAccessType.FIELD)
public class Student implements Serializable{
    @Id
    @Column(name = "student_id")
    @XmlElement(name = "studentID")
    private Long studentID;
    @Column(name = "email")
    @XmlElement(name = "email")
    private String email;
    @Column(name = "password")
    @XmlElement(name = "password")
    private String password;

    @Column(name = "last_name")
    @XmlElement(name = "lastName")
    private String lastName;
    @Column(name = "first_name")
    @XmlElement(name = "firstName")
    private String firstName;
    @Column(name = "dob")
    @JsonFormat(shape=JsonFormat.Shape.STRING, pattern = "yyyy/MM/dd")
    @XmlElement(name = "dob")
    private Date dob;
    @Column(name = "home_phone")
    @XmlElement(name = "homePhone")
    private String homePhone;
    @Column(name = "mobile")
    @XmlElement(name = "mobile")
    private String mobile;
    @Column(name = "first_day_on_campus")
    @JsonFormat(shape=JsonFormat.Shape.STRING, pattern = "yyyy/MM/dd")
    @XmlElement(name = "firstDayOnCampus")
    private Date firstDayOnCampus;

    @Column(name = "student_level")
    @XmlElement(name = "studentLevel")
    private String studentLevel;
    @Column(name = "gpa")
    @XmlElement(name = "gpa")
    private double gpa;
    @Column(name = "sat_score")
    @XmlElement(name = "satScore")
    private int satScore;
    @Column(name = "act_score")
    @XmlElement(name = "actScore")
    private int actScore;
    @Column(name = "last_login_date")
    @JsonFormat(shape=JsonFormat.Shape.STRING, pattern = "yyyy/MM/dd")
    @XmlElement(name = "lastLoginDate")
    private Date lastLoginDate;
    @Column(name = "last_login_ip")
    @XmlElement(name = "lastLoginIP")
    private String lastLoginIP;

    @OneToMany(mappedBy = "student")
    @XmlTransient
    private Set<Admissions> admissions;

    @OneToMany(mappedBy = "student")
    @XmlTransient
    private Set<Attendance> attendance;

    @OneToMany(mappedBy = "student")
    @XmlTransient
    private Set<CourseRoster> courseRoster;

    @OneToMany(mappedBy = "student")
    @XmlTransient
    private Set<ExamResultsStudentView> examResultsStudentView;

    @OneToMany(mappedBy = "student")
    @XmlTransient
    private Set<ExamResultsTeacherView> examResultsTeacherView;

    @OneToMany(mappedBy = "student")
    @XmlTransient
    private Set<GradeLevel> gradeLevel;

    @OneToMany(mappedBy = "student")
    @XmlTransient
    private Set<HomeworkAssignmentResultsStudentView> homeworkAssignmentResultsStudentView;

    @OneToMany(mappedBy = "student")
    @XmlTransient
    private Set<HomeworkAssignmentResultsTeacherView> homeworkAssignmentResultsTeacherView;

    @OneToMany(mappedBy = "student")
    @XmlTransient
    private Set<StudentDashboardSnapshotInfo> studentDashboardSnapshotInfo;

    @OneToMany(mappedBy = "student")
    @XmlTransient
    private Set<ReportCard> reportCard;

    @OneToMany(mappedBy = "student")
    @XmlTransient
    private Set<StudentDirectory> studentDirectory;

    @OneToMany(mappedBy = "student")
    @XmlTransient
    private Set<StudentHasParent> studentHasParent;

    @OneToMany(mappedBy = "student")
    @XmlTransient
    private Set<StudentSchedule> studentSchedule;

    @OneToMany(mappedBy = "student")
    @XmlTransient
    private Set<TeacherViewAllGradesInCourse> teacherViewAllGradesInCourse;

    @OneToMany(mappedBy = "student")
    @XmlTransient
    private Set<Transcripts> transcripts;

//constructors
//Getters and Setters

Student Service Class

@Service
public class StudentService {

    @Autowired
    StudentRepository studentRepository;

    //Create
    public Student createStudent(Student std) {
        return studentRepository.save(std);
    }

    //Read
    public List<Student> getStudent() {
        return studentRepository.findAll();
    }

    //Update
    public Student updateStudent(Long studentID, Student studentInformation) {
        Student std = studentRepository.findById(studentID).get();
        std.setEmail(studentInformation.getEmail());
        std.setPassword(studentInformation.getPassword());
        std.setLastName(studentInformation.getLastName());
        std.setFirstName(studentInformation.getFirstName());
        std.setDob(studentInformation.getDob());
        std.setHomePhone(studentInformation.getHomePhone());
        std.setMobile(studentInformation.getMobile());
        std.setFirstDayOnCampus(studentInformation.getFirstDayOnCampus());
        std.setGpa(studentInformation.getGpa());
        std.setSatScore(studentInformation.getSatScore());
        std.setActScore(studentInformation.getActScore());
        std.setLastLoginDate(studentInformation.getLastLoginDate());
        std.setLastLoginIP(studentInformation.getLastLoginIP());

        return studentRepository.save(std);
    }

    //Delete
    public void deleteStudent(Long studentID) {
        studentRepository.deleteById(studentID);
    }
}

Student Controller Class

@RestController
@RequestMapping("/studentsApi")
public class StudentController {
    @Autowired
    StudentService stdService;

    @Autowired
    StudentRepository stdRepo;

    @RequestMapping(value = "students", method = RequestMethod.POST)
    public Student createStudents(@RequestBody Student std) {
        return stdService.createStudent(std);
    }

    @RequestMapping(value = "students", method = RequestMethod.GET)
    public List<Student> readStudents() {
        return stdService.getStudent();
    }

    @RequestMapping(value = "students/{stdId}", method = RequestMethod.PUT)
    public Student updateStudents(@PathVariable(value = "stdId") Long id, @RequestBody Student stdDetails) {
        return stdService.updateStudent(id, stdDetails);
    }

    @RequestMapping(value = "students/{stdId}", method = RequestMethod.DELETE)
    public void deleteStudents(@PathVariable(value = "stdId") Long id) {
        stdService.deleteStudent(id);
    }

}

Student XML File

<student>
        <studentID>17365429</studentID>
        <email>[email protected]</email>
        <password>goodgrief72</password>
        <lastName>Brown</lastName>
        <firstName>Charlie</firstName>
        <dob>2008-10-09</dob>
        <homePhone>5053152585</homePhone>
        <mobile>2025550186</mobile>
        <firstDayOnCampus>2022-09-11</firstDayOnCampus>
        <studentLevel>Freshman</studentLevel>
        <gpa>0.0</gpa>
        <satScore>0</satScore>
        <actScore>0</actScore>
        <lastLoginDate>2022-06-06</lastLoginDate>
        <lastLoginIP>165.220.147.72</lastLoginIP>
    </student>

CodePudding user response:

I believe you need to review the correct payload to be sent to the endpoint.. here is an example that worked for me using your code:

{
"studentID": 1,
"email": "[email protected]",
"password": "pwdexample**",
"lastName": "user last name",
"firstName": "firts name",
"dob": "1990/01/01",
"homePhone": "0000000000",
"mobile": "000000000",
"firstDayOnCampus": "2022/01/01",
"studentLevel": "test"
}

and here is the response from the server

{
"studentID": 1,
"email": "[email protected]",
"password": "pwdexample**",
"lastName": "user last name",
"firstName": "firts name",
"dob": "1990/01/01",
"homePhone": "0000000000",
"mobile": "000000000",
"firstDayOnCampus": "2022/01/01",
"studentLevel": "test",
"gpa": 0,
"satScore": 0,
"actScore": 0,
"lastLoginDate": null,
"lastLoginIP": null,
"admissions": null,
"attendance": null,
"courseRoster": null,
"examResultsStudentView": null,
"examResultsTeacherView": null,
"gradeLevel": null,
"homeworkAssignmentResultsStudentView": null,
"homeworkAssignmentResultsTeacherView": null,
"studentDashboardSnapshotInfo": null,
"reportCard": null,
"studentDirectory": null,
"studentHasParent": null,
"studentSchedule": null,
"teacherViewAllGradesInCourse": null}
  • Related