无法将字符串化为Java对象 - 无法构造`eardvely'的实例
我有一个字符串,我应该将其阅读到地址对象中。 现在,我将获得的字符串可能包含一些额外的字段,并且可能不包含我地址类中声明的某些字段。
我已经在地址类上添加了以下注释 -
@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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您面临的错误基于 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 anAddress
from the single string value you pass in. It does not read the string contents and set field values. If you need to useconvertValue
, then you have to create a constructor likepublic 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