【发布时间】:2021-03-27 20:33:22
【问题描述】:
Spring + Thymeleaf 项目的本质描述如下:
@Entity
public class CarEntity {
...
@Column(name = "time_oot", nullable = false)
//@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "hh:mm:ss")
//@DateTimeFormat(pattern = "hh:mm:ss")
private LocalTime timeOut;
}
我们从 HTML 表单中读取本地时间格式的时间。我没有几秒钟就得到了输出,一切正常。即表单输入属于这种类型:
<label for="inputime">Время</label>
<input type="time" class="form-control" id="inputime" placeholder="Задайте время" max="0:50:00" min="0:00:01" value="00:00" th:field="*{timeOut}" required>
但我不需要表格上的小时,只需要分钟和秒,我在表格上添加了秒:
<label for="inputime">Время</label>
<input type="time" class="form-control" id="inputime" placeholder="Задайте время" max="0:50:00" min="0:00:01" value="00:00:00" step="1" th:field="*{timeOut}" required>
现在错误开始飞出:
Field error in object 'CarEntity' on field 'timeOut': rejected value [00:12:25];
codes
[typeMismatch.CarEntity.timeOut,typeMismatch.timeOut,typeMismatch.java.time.LocalTime,typeMismatch];
arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [CarEntity.timeOut,timeOut];
arguments []; default message [timeOut]];
default message [Failed to convert property value of type 'java.lang.String' to required type 'java.time.LocalTime' for property 'timeOut';
nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String]
to type [@javax.persistence.Column java.time.LocalTime] for value '00:12:25'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [00:12:25]]]
有什么问题?
我尝试添加:
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "hh:mm:ss")
或
@DateTimeFormat(pattern = "hh:mm:ss")
错误仍然崩溃:
"Parse attempt failed for value [01:11:23]"
【问题讨论】:
标签: java spring-boot thymeleaf