Setter for LocalDateTime (Java 8 API) 被调用两次
Setter for LocalDateTime (Java 8 API) gets called twice
我已经为我正在开发的 Java Web 应用程序实现了 class。 class 有一个 LocalDateTime 属性 'created'。但是,当我尝试设置 属性 (一次)时,它的 setter 以某种方式连续调用两次 - 首先设置我想要的值,然后在第二次调用时将其设置为 null 甚至不应该发生。
我已经通过以下方法进行了跟踪,直到第三行,一切看起来都很好。
public static ICEDocument mapDocumentFromSOLR(SolrDocument document) {
ICEDocument result = new ICEDocument();
Date uploaded = (Date) document.getFieldValue("CREATED");
LocalDateTime uploadDate = LocalDateUtils.convertUtcDateToLocalDateTime(uploaded); // custom class
result.setCreated(uploadDate); // **faulty line**
}
这里是 class,为清楚起见缩短了:
import java.time.LocalDateTime;
import org.springframework.data.annotation.Transient;
[...]
@JsonIgnoreProperties(ignoreUnknown=true)
public class ICEDocument implements java.io.Serializable {
[...]
@Transient
private LocalDateTime created;
[...]
@JsonDeserialize(using=LocalDateTimeJsonDeserializer.class)
public void setCreated(LocalDateTime created) {
System.out.println("Setting creation date " + created); // added for debugging
this.created = created;
}
}
我为解决此问题采取的步骤
删除@Transient。数据是通过Hibernate(ver5.1)填入的,我最初注解的是属性,因为这个字段本身不在对应的数据库table中。我认为这可能是问题所在(参见 Object Serialization and Java Transient Variables),但删除它并没有改变任何东西。
正在更改第三行。我将它与静态 LocalDateUtils 方法中的基本内容进行了切换。这没有解决问题。
LocalDateTime uploadDate = uploaded.toInstant().atZone(ZoneId.of("UTC")).toLocalDateTime();
删除 JSON 解串器。我不认为 JsonDeserializer 有问题,因为此时它不应该(并且不符合 Debug)做任何事情,但为了完整起见,我将在此处添加它。可能我现在只是在抓住救命稻草。
public class LocalDateTimeJsonDeserializer extends JsonDeserializer<LocalDateTime> {
private static final String DATE_TIME = "yyyy-MM-dd' 'HH:mm:ss";
@Override
public LocalDateTime deserialize(JsonParser parser, DeserializationContext context)
throws IOException, JsonProcessingException {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_TIME);
LocalDateTime deserializedDate = LocalDateTime.parse(parser.getText(), formatter);
return deserializedDate;
}
}
感谢您阅读我相当长的 post 的结尾。
在调试代码后,我发现更下方的一行将 属性 设置为空。所以我想这实际上是对 setter 的第二次调用,而且运气不好。
但是知道其他因素没有任何问题可能会有所帮助,所以我就把它留在这里。再次感谢。
我已经为我正在开发的 Java Web 应用程序实现了 class。 class 有一个 LocalDateTime 属性 'created'。但是,当我尝试设置 属性 (一次)时,它的 setter 以某种方式连续调用两次 - 首先设置我想要的值,然后在第二次调用时将其设置为 null 甚至不应该发生。
我已经通过以下方法进行了跟踪,直到第三行,一切看起来都很好。
public static ICEDocument mapDocumentFromSOLR(SolrDocument document) {
ICEDocument result = new ICEDocument();
Date uploaded = (Date) document.getFieldValue("CREATED");
LocalDateTime uploadDate = LocalDateUtils.convertUtcDateToLocalDateTime(uploaded); // custom class
result.setCreated(uploadDate); // **faulty line**
}
这里是 class,为清楚起见缩短了:
import java.time.LocalDateTime;
import org.springframework.data.annotation.Transient;
[...]
@JsonIgnoreProperties(ignoreUnknown=true)
public class ICEDocument implements java.io.Serializable {
[...]
@Transient
private LocalDateTime created;
[...]
@JsonDeserialize(using=LocalDateTimeJsonDeserializer.class)
public void setCreated(LocalDateTime created) {
System.out.println("Setting creation date " + created); // added for debugging
this.created = created;
}
}
我为解决此问题采取的步骤
删除@Transient。数据是通过Hibernate(ver5.1)填入的,我最初注解的是属性,因为这个字段本身不在对应的数据库table中。我认为这可能是问题所在(参见 Object Serialization and Java Transient Variables),但删除它并没有改变任何东西。
正在更改第三行。我将它与静态 LocalDateUtils 方法中的基本内容进行了切换。这没有解决问题。
LocalDateTime uploadDate = uploaded.toInstant().atZone(ZoneId.of("UTC")).toLocalDateTime();
删除 JSON 解串器。我不认为 JsonDeserializer 有问题,因为此时它不应该(并且不符合 Debug)做任何事情,但为了完整起见,我将在此处添加它。可能我现在只是在抓住救命稻草。
public class LocalDateTimeJsonDeserializer extends JsonDeserializer<LocalDateTime> { private static final String DATE_TIME = "yyyy-MM-dd' 'HH:mm:ss"; @Override public LocalDateTime deserialize(JsonParser parser, DeserializationContext context) throws IOException, JsonProcessingException { DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_TIME); LocalDateTime deserializedDate = LocalDateTime.parse(parser.getText(), formatter); return deserializedDate; } }
感谢您阅读我相当长的 post 的结尾。
在调试代码后,我发现更下方的一行将 属性 设置为空。所以我想这实际上是对 setter 的第二次调用,而且运气不好。
但是知道其他因素没有任何问题可能会有所帮助,所以我就把它留在这里。再次感谢。