Reputation: 441
I am trying to edit an exam object but I am getting this error: Failed to read HTTP message. Required request body is missing.
I believe the error is that You can't send a request body with an HTTP GET request but I don't know how to do it instead.
The user selects an exam to edit and I want the HTML to pass that examId to the controller.
My Controller:
@RequestMapping(value = "/editExam.html{examId}", method = {
RequestMethod.GET, RequestMethod.PUT })
public String editExam(@ModelAttribute("exam") @PathVariable(value =
"examId")Long examId, @RequestBody Exam exam,Model model, BindingResult
result) {
examRepository.findOne(examId);
model.addAttribute("examTitle", exam.getExamTitle());
model.addAttribute("examGradeWorth", exam.getExamGradeWorth());
model.addAttribute("examGradeAchieved", exam.getExamGradeAchieved());
exam.setExamTitle(exam.getExamTitle());
exam.setExamGradeWorth(exam.getExamGradeWorth());
exam.setExamGradeAchieved(exam.getExamGradeAchieved());
examRepository.save(exam);
return "editExam";
}
editExam.html:
<form action="#" th:action="@{/editExam.html{examId}}" th:object="${exam}" method="put">
<table>
<tr>
<td> Exam Title:</td>
<td><input type="text" th:field="*{examTitle}" th:text="${exam.examTitle}"/></td>
<!-- <td th:if="${#fields.hasErrors('examTitlee')}" th:errors="*{examTitle}">error message</td> -->
</tr>
<tr>
<td> Exam grade worth </td>
<td><input th:field="*{examGradeWorth}" /></td>
<!-- <td th:if="${#fields.hasErrors('examGradeWorth')}" th:errors="*{examGradeWorth}">error message</td> -->
</tr>
<tr>
<td>examGradeAchieved</td>
<td><input th:field="*{examGradeAchieved}"/></td>
</tr>
<tr>
<td><button type="submit">Submit post</button></td>
</tr>
</table>
Upvotes: 0
Views: 8725
Reputation: 601
As per best-practices of designing/creating REST APIs, it is advisable that...
POST
HTTP methodPUT
HTTP methodGET
HTTP methodDELETE
HTTP methodSo in your case, you should use PUT
HTTP method instead of GET
while you are updating a resource (exam in your case), and anyway, GET
HTTP method does not allow user to add a request-body in HTTP request.
Upvotes: 1
Reputation: 441
I fixed the error by changing the start of the controller to this:
@RequestMapping(value = "/editExam.html/id={examId}", method = { RequestMethod.GET , RequestMethod.POST, RequestMethod.PUT})
Upvotes: 0