2
我创建了一个采用事件表单对象的Spring引导控制器。将序列化的HTML时间字段转换为java.time.LocalTime
@RestController
@RequestMapping("/Event")
public class EventController {
@RequestMapping(value = "/create", method = RequestMethod.POST)
private synchronized List<Event> createEvent(Event inEvent) {
log.error("create called with event: " + inEvent);
create(inEvent);
return listAll();
}
}
Event类看起来像这样(省略getter/setter方法)
public final class Event {
private Integer id;
private Integer periodId;
private String name;
@DateTimeFormat(pattern = "dd/MM/yyyy")
private LocalDate eventDate;
private LocalTime startTime;
private LocalTime endTime;
private Integer maxParticipants;
private String status;
private String eventType;
}
我得到的开始时间Spring的类型不匹配错误和endTime领域
Field error in object 'event' on field 'endTime': rejected value
[12:00] codes
[typeMismatch.event.endTime,typeMismatch.endTime,typeMismatch.java.time.LocalTime,typeMismatch]
arguments
[org.springframework.context.support.DefaultMessageSourceResolvable:
codes [event.endTime,endTime] arguments [] default message [endTime]]
default message [Failed to convert property value of type
'java.lang.String' to required type 'java.time.LocalTime' for property
'endTime' nested exception is
org.springframework.core.convert.ConversionFailedException: Failed to
convert from type [java.lang.String] to type [java.time.LocalTime] for
value '12:00' nested exception is java.lang.IllegalArgumentException:
Parse attempt failed for value [12:00]]
序列化格式数据使用jQuery AJAX方法发布。序列化的数据如下所示:
eventDate=27%2F01%2F2017&eventType=REN&maxParticipants=10&startTime=09%3A00&endTime=12%3A00
如何让Spring正确解析序列化的时间字段?
我使用Java 8
为我节省了很多时间。 Upvoting。请添加相关内容(书籍或教程)。谢谢,。 –
@Vito,感谢您的加入!我用来回答这个问题的唯一资源是已经在答案中链接的JavaDoc页面。就更普遍的阅读材料而言,我一直认为公开的[Spring文档](https://spring.io/docs)非常好。 –