我有一個(gè)類型的對(duì)象User(如下定義),它在序列化為 Json 時(shí)會(huì)引發(fā)此特定錯(cuò)誤:com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: com.providenceuniversal.gim.AuthenticationRequest["user"]->com.providenceuniversal.gim.User["lastSeenToLocal"])User定義(具有相關(guān)屬性和方法):public class User implements ServerMessage { //..... private final @JsonInclude(Include.NON_NULL) Instant lastSeen; @JsonCreator User(@JsonProperty("username") String username) { if (!isValidUsername(username)) throw new IllegalArgumentException("Invalid constructor argument values"); this.username = username.trim(); this.firstName = null; this.lastName = null; this.email = null; this.status = null; this.joinDate = null; this.lastSeen = null; this.dateOfBirth = null; } @JsonCreator User( @JsonProperty("username") String username, @JsonProperty("firstName") String firstName, @JsonProperty("lastName") String lastName, @JsonProperty("email") String email, @JsonProperty("status") String status, @JsonProperty("joinDate") Instant joinDate, @JsonProperty("lastSeen") Instant lastSeen, @JsonProperty("dateOfBirth") LocalDate dateOfBirth) { if (username == null || username.trim().length() == 0) throw new IllegalArgumentException("Invalid constructor argument values"); this.username = username.trim(); this.firstName = firstName; this.lastName = lastName; this.email = email; this.status = status; this.joinDate = joinDate; this.lastSeen = lastSeen; this.dateOfBirth = dateOfBirth; }從異常中可以明顯看出問題是由getLastSeenToLocal()方法(它試圖在 中操作null對(duì)象lastSeen)引起的,我看不到它與序列化過程的相關(guān)性。Jackson 是否默認(rèn)調(diào)用所有 getter,無論它們返回的字段是否列為JsonPropertys,或者我缺少某些東西(顯然)?
將對(duì)象序列化為 Json 時(shí)出現(xiàn) NullPointerException
慕尼黑5688855
2022-05-25 10:25:18