Пытаюсь в форме выбрать дату, но контроллер кидает исключение.
Model
public class Session {
private int id;
private String name;
private String text;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDateTime created;
Controller
@GetMapping("/formAddSessions")
public String addSessions(Model model, HttpSession session) {
model.addAttribute("sessions",
new Session(0, "Заполните поле", "Заполните поле", null));
sessions(model, session);
return "addSessions";
}
@RequestMapping(value = "/createSession", method = RequestMethod.POST)
public String createSessions(@ModelAttribute Session sessions) {
Optional<Session> sess = this.ses.create(sessions);
if (sess.isEmpty()) {
return "redirect:/formAddSessions";
}
return PATH;
}
Form
<form th:action="@{/createSession}" enctype="multipart/form-data" method="POST" th:object="${sessions}">
<input type="hidden" name="id" value="0"/>
<div class="form-group">
<label for="name">Имя</label>
<input type="text" class="form-control" name="name" id="name">
</div>
<div class="form-group">
<label for="text">Описание</label>
<input type="text" class="form-control" name="text" id="text">
</div>
<div class="form-group">
<label for="created">Установить время начала трансляции</label>
<input type="date" id="created" th:field="*{{create}}"/>
</div>
<div class="main">
<button type="submit" class="btn btn-dark">Сохранить</button>
</div>
</form>
Ошибка
Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors Field error in object 'session' on field 'create': rejected value [2022-09-11]; codes [typeMismatch.session.create,typeMismatch.create,typeMismatch.java.time.LocalDateTime,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [session.create,create]; arguments []; default message [create]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.time.LocalDateTime' for property 'create'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.time.LocalDateTime] for value '2022-09-11'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2022-09-11]]]
org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.time.LocalDateTime] for value '2022-09-11
говорит, что фэйлится конвертация. Тут есть аналогичное решение.