【发布时间】:2019-07-17 08:32:10
【问题描述】:
我想将没有时间的日期存储到我的数据库中。所以,我选择使用LocalDate 类型。
如this article 中所述,我使用JPA 转换器将LocalDate 转换为Date。
但是,当我想持久化我的实体(使用 POST 和 PUT 请求)时,我遇到了一些麻烦。
错误
2019-02-23 11:26:30.254 WARN 2720 --- [-auto-1-exec-10] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Expected array or string.; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Expected array or string.
at [Source: (PushbackInputStream); line: 1, column: 104] (through reference chain: ...entity.MyObject["startdate"])]
org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class org.springframework.http.ResponseEntity]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `org.springframework.http.ResponseEntity` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (PushbackInputStream); line: 1, column: 2]
代码
转换器
package ...entity;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.time.LocalDate;
import java.sql.Date;
@Converter(autoApply = true)
public class LocalDateAttributeConverter implements AttributeConverter<LocalDate, Date> {
@Override
public Date convertToDatabaseColumn(LocalDate locDate) {
return (locDate == null ? null : Date.valueOf(locDate));
}
@Override
public LocalDate convertToEntityAttribute(Date sqlDate) {
return (sqlDate == null ? null : sqlDate.toLocalDate());
}
}
实体
package ...entity;
import org.hibernate.annotations.ColumnDefault;
import javax.persistence.*;
import java.time.LocalDate;
import java.util.HashSet;
import java.util.Set;
@Entity
public class MyObject {
@Id
private String id;
private LocalDate startdate;
private LocalDate enddate;
public MyObject() {}
public MyObject(LocalDate enddate) {
this.startdate = LocalDate.now();
this.enddate = enddate;
}
...
}
“主要”
private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
MyObject myobject = new MyObject(LocalDate.parse("2019-03-01", formatter));
感谢您的帮助。
编辑 1:MyObject 的打印
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<>(this.toJsonString(myObject), headers);
System.out.println(entity.toString());
// <{"id":"ba6649e4-6e65-4f54-8f1a-f8fc7143b05a","startdate":{"year":2019,"month":"FEBRUARY","dayOfMonth":23,"dayOfWeek":"SATURDAY","era":"CE","dayOfYear":54,"leapYear":false,"monthValue":2,"chronology":{"id":"ISO","calendarType":"iso8601"}},"enddate":{"year":2019,"month":"MARCH","dayOfMonth":1,"dayOfWeek":"FRIDAY","era":"CE","dayOfYear":60,"leapYear":false,"monthValue":3,"chronology":{"id":"ISO","calendarType":"iso8601"}}},[Content-Type:"application/json"]>
【问题讨论】:
-
所以你在 JSON 中有一个错误,它不是 JPA。即使是 JPA,您也需要提及您使用的是哪个 JPA 提供程序,以及您认为该 LocalDate 没有得到正确处理的地方。你的调试说明了什么
-
@BillyFrost 我编辑了我的问题。我在
JSON中打印myObject。格式不是我的格式化程序中指定的格式... -
这对隔离问题所在没有任何作用。您在 JPA 中的问题是没有将数据持久化到您的数据库中,或者没有从您的数据库中检索数据....还是只是将 Java 对象转换为 JSON ?????如果问题存在于持久性中,则定义用于持久化的 SQL 以及您认为该 SQL 存在问题的位置......并且“持久”不涉及 POST / PUT ...它涉及 JPA em.persist
-
所以,我不明白为什么
sysout()的格式不是yyyy-MM-dd。因为如果我这样做sysout(myObject.getStartdate()),结果是yyyy-MM-dd格式的日期... -
好的,那么与 JPA 无关。一个
LocalDate.toString() 方法有它自己的由JAVADOCS 定义的输出格式,按照docs.oracle.com/javase/8/docs/api/java/time/… 与“问题”有什么关系?
标签: java spring spring-boot java-8 spring-data-jpa