在poster中提交了一个post请求,仅映射了4个实体(包括主键)



我最近尝试通过poster发送一个post请求,但并不是所有内容都映射正确。尽管我在json文件中为每个属性设置了值,但它只将值映射到4个属性。其他属性被标记为null。它还做了一些奇怪的事情,显示了我数据库中其他实体的值。我认为这个问题在某种程度上与此有关。下面我有一张之前的截图,以及我提交请求时得到的回复。我还添加了模型、服务和控制器类,以及我为学生实体转换为JSON的xml文件。如果有任何帮助,我将不胜感激。

之前

响应

学生模型类

@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

学生服务类

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

学生控制器类

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

学生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>

我认为您需要查看要发送到端点的正确有效负载。。这里有一个使用你的代码对我有效的例子:

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

这是来自服务器的响应

{
"studentID": 1,
"email": "email@example.com",
"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}

最新更新