簡體   English   中英

“JSON 解析錯誤:無法構造實例(盡管至少存在一個 Creator):無法從 Object 值反序列化 - SpringBoot

[英]"JSON parse error: Cannot construct instance of (although at least one Creator exists): cannot deserialize from Object value - SpringBoot

我正在將 SpringBoot 與 Mongo 數據庫一起使用,並且我正在嘗試將嵌入式文檔保存到數據庫中。

我有這個 model:

型材.java

@Data
@Document
public class Profile {

    public final City city;
    public final String imageId;

    public Profile(City city,
                   String imageId) {
        this.city = city;
        this.imageId = imageId;
    }

    @Override
    public String toString() {
        return "Profile{" +
                ", city=" + city +
                ", imageId='" + imageId + '\'' +
                '}';
    }

    private static boolean atLeast(int numChars, String s) {
        if (s == null) {
            return false;
        }
        var str = s.strip();
        return str.length() >= numChars;
    }


    public static ProfileBuilder builder() {
        return new ProfileBuilder();
    }

    public static final class ProfileBuilder {
        public City city;
        public String imageId;

        private ProfileBuilder() {
        }


        public ProfileBuilder withCity(City city) {
            this.city = city;
            return this;
        }

        public ProfileBuilder withImageId(String imageId) {
            this.imageId = imageId;
            return this;
        }

        public Profile build(){
            return new Profile(city, imageId);
        }
    }
}

城市.java

public class City {

    public final String name;

    public City(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "City{" +
                ", name='" + name + '\'' +
                '}';
    }
}

ProfileController.java

 @RequestMapping( method = RequestMethod.POST)
    public Profile addUser(@RequestBody Profile profile) {
        return profileService.addProfile(profile);
    }

和 postman 我發送這個 JSON

{
 "city":{
   "name":"Atena"
  },
   "imageId" : "Doe",
  }
}

但我收到以下錯誤:

"JSON parse error: Cannot construct instance of `domain.City` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator);"

至少有兩種解決方案。

  1. @JsonCreator添加到構造函數並將@JsonProperty添加到其參數(以指示 Jackson 如何以正確的順序將 JSON 項替換到構造函數中)
class Profile {
    ...
    @JsonCreator
    public Profile(@JsonProperty("city") City city, 
                   @JsonProperty("imageId") String imageId) {
        this.city = city;
        this.imageId = imageId;
    }
    ...
}

(+ 城市類相同)

  1. Un final類屬性並提供默認的無參數構造函數(以及現有的全參數構造函數)。
class Profile {

    public City city;
    public String imageId;

    public Profile() {
    }

    public Profile(City city, String imageId) {
        this.city = city;
        this.imageId = imageId;
    }
}

(+ 城市類相同)

測試

class Test {
    public static void main(String[] args) throws JsonProcessingException {
        String json = "{\"city\":{\"name\":\"Atena\"},\"imageId\":\"Doe\"}";
        Profile p = new ObjectMapper().readValue(json, Profile.class);
        System.out.println(p);
    }
}

輸出:

Profile{, city=City{, name='Atena'}, imageId='Doe'}

在只有一個屬性的類中,要反序列化 object Json 需要來自該 class 的 nos 參數構造函數。

在您的 class 城市中,您需要一個 nos arg 構造函數,將其添加到您需要的 class 中:

 public  City () {}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM