简体   繁体   中英

Setter for LocalDateTime (Java 8 API) gets called twice

I've implemented a class for a Java Web App I'm working on. The class has a LocalDateTime property 'created'. However, when I try to set that property (once), its setter is somehow called twice in succession - first setting the value I want, then setting it to null on a second call that should not even happen.

I've traced through the following method and everything looks well up to the third line.

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**
}

Here's the class, shortened for clarity:

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;
}
} 

Steps I've taken trying to resolve this

  1. Removing the @Transient . The data is filled in via Hibernate (ver5.1), and I originally annotated the property since the field itself is not in the corresponding database table. I thought that might be the problem (see Object Serialization and Java Transient Variables ), but removing it didn't change anything.

  2. Changing the third line . I switched it with what was basically inside the static LocalDateUtils method. This didn't resolve the issue.

     LocalDateTime uploadDate = uploaded.toInstant().atZone(ZoneId.of("UTC")).toLocalDateTime(); 
  3. Removing the JSON Deserializer . I don't think the JsonDeserializer is at fault since it isn't supposed to (and doesn't accd. to Debug) do anything at this point, but I'll add it here for completeness sake. Could be I'm just grasping at straws at this point.

     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; } } 

Thank you for reading to the end of my rather long post.

After debugging the code I found a line further down that set the property to null. So it was in fact a second call to the setter and a lot of bad luck, I suppose.

But it might help to know that there wasn't anything wrong with the other factors, so I"ll just leave this here. Thanks again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM