“JSON 解析错误:无法构造实例(尽管至少存在一个 Creator):无法从对象值反序列化 - SpringBoot

use*_*049 3 java mongodb spring-boot

我正在将 SpringBoot 与 Mongo 数据库一起使用,并且尝试将嵌入文档保存到数据库中。

我有这个模型:

配置文件.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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

城市.java

public class City {

    public final String name;

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

    @Override
    public String toString() {
        return "City{" +
                ", name='" + name + '\'' +
                '}';
    }
}
Run Code Online (Sandbox Code Playgroud)

ProfileController.java

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

我和邮递员一起发送这个 JSON

{
 "city":{
   "name":"Atena"
  },
   "imageId" : "Doe",
  }
}
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误:

"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);"
Run Code Online (Sandbox Code Playgroud)

小智 7

在只有一个属性的类中,要反序列化 Json 对象,需要该类的 nos 参数构造函数。

在你的类城市中,你需要一个 nos arg 构造函数,将其添加到你需要的类中:

 public  City () {}
Run Code Online (Sandbox Code Playgroud)


Nik*_*nko 6

至少有两种解决方案。

  1. 添加@JsonCreator到构造函数及其@JsonProperty参数(指示 Jackson 如何以正确的顺序将 JSON 项替换到构造函数中)
class Profile {
    ...
    @JsonCreator
    public Profile(@JsonProperty("city") City city, 
                   @JsonProperty("imageId") String imageId) {
        this.city = city;
        this.imageId = imageId;
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

(+ 城市舱同样)

  1. Unfinal类属性并提供默认的无参数构造函数(以及现有的全参数构造函数)。
class Profile {

    public City city;
    public String imageId;

    public Profile() {
    }

    public Profile(City city, String imageId) {
        this.city = city;
        this.imageId = imageId;
    }
}
Run Code Online (Sandbox Code Playgroud)

(+ 城市舱同样)

测试

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

Profile{, city=City{, name='Atena'}, imageId='Doe'}
Run Code Online (Sandbox Code Playgroud)