无法将字符串化为Java对象 - 无法构造`eardvely'的实例

发布于 2025-01-27 17:14:26 字数 1270 浏览 3 评论 0原文

我有一个字符串,我应该将其阅读到地址对象中。 现在,我将获得的字符串可能包含一些额外的字段,并且可能不包含我地址类中声明的某些字段。

我已经在地址类上添加了以下注释 -

@JsonInclude(NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Address {
   private String line1;
   private String line2;
   private String state;
   private String pincode;
......
}

但是在这样的字符串时 - {“街”:“街”,“ line1”:“ line1”,“ line2”:“ line2”,“ district”:“ district”:“ pincode”:“ pincode”},遇到此错误 -

java.lang.IllegalArgumentException: Cannot construct instance of `Address` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{"street":"street","line1":"line1","line2":"line2","district":"district","pincode":"pincode"}')
 at [Source: UNKNOWN; line: -1, column: -1]

已经添加了以下config对我的对象映射器 -

objectmapper.configure(deserializationfeature.fail_on_unknown_properties, 错误的); objectmapper.configure(deserializationfeature.fail_on_missing_creator_properties, 错误的); objectMapper.setSerializationInclusion(jsoninclude.include.non_null);

转换逻辑 -

String addressDetail = info.get("address");
Address address = objectMapper.convertValue(addressDetail, Address.class);

I have a string which I'm supposed to read into an address object.
Now the string I'll get may contain some extra fields and may not contain some fields which is declared in my Address class.

I've already added following annotations on my address class -

@JsonInclude(NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Address {
   private String line1;
   private String line2;
   private String state;
   private String pincode;
......
}

But while deserializing a string like this -
{"street":"street","line1":"line1","line2":"line2","district":"district","pincode":"pincode"}, getting this error -

java.lang.IllegalArgumentException: Cannot construct instance of `Address` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{"street":"street","line1":"line1","line2":"line2","district":"district","pincode":"pincode"}')
 at [Source: UNKNOWN; line: -1, column: -1]

Already added following config to my object mapper -

objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
false);
objectMapper.configure(DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES,
false);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

Conversion logic -

String addressDetail = info.get("address");
Address address = objectMapper.convertValue(addressDetail, Address.class);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兲鉂ぱ嘚淚 2025-02-03 17:14:26

您面临的错误基于 objectmmapper crevertvalue-value-value-value-readvalue

基本上,什么objectmapper.convertvalue(addressDetail,adversion.class)尝试,是从您传递的单字符串值中创建address。并设置字段值。如果您需要使用convertValue,则必须创建一个构造函数,例如public dordment(String json){/*手动在此处手动映射*/}

但是,更好的方法是使用ObjectMapper.ReadValue(adversionDetail,adversion.Class),因为此方法实际上遍历JSON字符串并使用字符串中存在的字段创建一个实例。 ReadValue仅需要Getter/setter和一个no-args构造函数才能函数

The error you are facing is based on objectmapper convertvalue-vs-readvalue.

Basically, what objectMapper.convertValue(addressDetail, Address.class) tries, is to create an Address from the single string value you pass in. It does not read the string contents and set field values. If you need to use convertValue, then you have to create a constructor like public Address(String json){/*do mapping manually here*/}.

The better approach however would be to use objectMapper.readValue(addressDetail, Address.class), as this method actually traverses the JSON string and creates an instance with the fields you have present in the string. readValue only requires getter/setter and an no-args constructor to function

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文