Postman发送POST请求仅映射4个Student实体字段问题求助
字段映射异常求助:POST请求仅部分属性映射成功,响应返回错误数据
我用Postman发送POST请求时遇到问题:明明在JSON里给Student实体的所有属性都赋了值,但最终只有4个属性(包含主键)成功映射,其余属性全为null,而且响应还错误返回了数据库里其他实体的数据。
相关截图:
- 请求前截图
- 响应截图
Student实体类
@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; //构造方法 //Getter和Setter
Student服务类
@Service public class StudentService { @Autowired StudentRepository studentRepository; //新增 public Student createStudent(Student std) { return studentRepository.save(std); } //查询所有 public List<Student> getStudent() { return studentRepository.findAll(); } //更新 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); } //删除 public void deleteStudent(Long studentID) { studentRepository.deleteById(studentID); } }
Student控制器类
@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); } }
转换为JSON前的XML文件
<student> <studentID>17365429</studentID> <email>charliebrown@gmail.com</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>
内容的提问来源于stack exchange,提问作者Chris Barrood
相关产品推荐
相关产品推荐

