我正在尝试编辑考试对象但是我收到此错误: 无法读取HTTP消息。缺少必需的请求正文。
我认为错误在于您无法通过HTTP GET请求发送请求正文,但我不知道该怎么做。
用户选择要编辑的考试,我希望HTML将该考试传递给控制器。
我的控制器:
@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>
答案 0 :(得分:1)
根据设计/创建REST API的最佳实践,建议......
POST
HTTP方法PUT
HTTP方法GET
HTTP方法DELETE
HTTP方法因此,在您的情况下,在更新资源(在您的情况下为考试)时应使用PUT
HTTP方法而不是GET
,无论如何,GET
HTTP方法不会允许用户在HTTP请求中添加请求主体。
答案 1 :(得分:0)
我通过将控制器的启动更改为:
来修复错误@RequestMapping(value = "/editExam.html/id={examId}", method = { RequestMethod.GET , RequestMethod.POST, RequestMethod.PUT})