第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

將對(duì)象序列化為 Json 時(shí)出現(xiàn) NullPointerException

將對(duì)象序列化為 Json 時(shí)出現(xiàn) NullPointerException

慕尼黑5688855 2022-05-25 10:25:18
我有一個(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,或者我缺少某些東西(顯然)?
查看完整描述

1 回答

?
鴻蒙傳說

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊

Jackson 默認(rèn)使用類的 getters()。這樣你就可以

  • 使用 @JsonAutoDetect 使用字段

  • 將 @JsonIgnore 添加到 getLastSeenToLocal() 方法

  • 改進(jìn) getLastSeenToLocal() 以檢查相關(guān)字段不為空(或任何其他條件)


查看完整回答
反對(duì) 回復(fù) 2022-05-25
  • 1 回答
  • 0 關(guān)注
  • 306 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)