[英]Spring boot project with LocalDateTime
Can you use LocalDateTime with a Spring Boot project and if so how? 您可以在Spring Boot项目中使用LocalDateTime吗?
I tried to follow this post and added the dependancy and the line required in application.properties but I still get : 我试图按照这篇文章,并添加了依赖和application.properties中所需的行,但我仍然得到:
java.io.StreamCorruptedException: invalid stream header: 32303137
When persisting data or trying to view existing data with dates created using Java.Util.Date. 保留数据或尝试使用Java.Util.Date创建的日期查看现有数据时。
Ok, so I got it to go. 好的,我明白了。 It required multiple changes to make both Hibernate & Springboot & Thymeleaf all work with Java 8 - LocalDateTime.
它需要进行多项更改才能使Hibernate&Springboot&Thymeleaf都可以与Java 8-LocalDateTime一起使用。
Hibernate 过冬
Add dependencies: 添加依赖项:
compile("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.4.0")
compile group: 'org.hibernate', name: 'hibernate-java8'
Add the following to application.properties: 将以下内容添加到application.properties:
spring.jackson.serialization.write_dates_as_timestamps=false
The annotations on my entities look like: 我的实体上的注释如下:
@JsonFormat(pattern="yyyy-MM-dd")
@DateTimeFormat(iso = DateTimeFormat.ISO.TIME)
private LocalDateTime somedate;
Although that didn't seem to be strictly needed. 尽管这似乎并非绝对必要。
Thymeleaf Thymeleaf
add dependency: 添加依赖项:
compile group: 'org.thymeleaf.extras', name: 'thymeleaf-extras-java8time', version: '3.0.0.RELEASE'
Make sure it matches your Thymeleaf version. 确保它与您的Thymeleaf版本匹配。
In any HTML in the project your dates fields should now use #temporals instead of #dates. 在项目中的任何HTML中,您的日期字段现在都应该使用#temporals而不是#dates。 ie:
即:
<td th:text="${#temporals.format(object.somedate, 'yyyy-MM-dd HH:mm')}">12/12/2018</td>
Spring boot 春季靴
In my Application.java class I added: 在我的Application.java类中,添加了:
@Bean
public Java8TimeDialect java8TimeDialect() {
return new Java8TimeDialect();
}
The following resources were invaluable: 以下资源是无价的:
http://blog.codeleak.pl/2015/11/how-to-java-8-date-time-with-thymeleaf.html#comment-form (Thymeleaf/Springboot) http://blog.codeleak.pl/2015/11/how-to-java-8-date-time-with-thymeleaf.html#comment-form(Thymeleaf / Springboot)
https://www.thoughts-on-java.org/hibernate-5-date-and-time/ (Hibernate) https://www.thoughts-on-java.org/hibernate-5-date-and-time/ (休眠)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.