没有要从字符串值('')反序列化的字符串参数构造函数/工厂方法
使用包中的ObjectMapper
类时遇到了json解析问题com.fasterxml.jackson.databind
,而我得到的错误是:
com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.graybar.utilities.ups.beans.Address: no String-argument constructor/factory method to deserialize from String value ('')
发生此问题的Web应用程序是使用AngularJS前端的Spring MVC应用程序,但我可以使用更小的所有Java程序来复制该问题。这是我的豆子:
Shipment.java
@JsonIgnoreProperties(ignoreUnknown = true)public class Shipment {
@JsonProperty("Activity")
private ArrayList<Activity> activity;
public ArrayList<Activity> getActivity() {
return activity;
}
public void setActivity(ArrayList<Activity> activity) {
this.activity = activity;
}
}
Activity.java
@JsonIgnoreProperties(ignoreUnknown = true)public class Activity {
@JsonProperty("ActivityLocation")
private ArrayList<ActivityLocation> activityLocation;
public ArrayList<ActivityLocation> getActivityLocation() {
return activityLocation;
}
public void setActivityLocation(ArrayList<ActivityLocation> activityLocation) {
this.activityLocation = activityLocation;
}
}
ActivityLocation.java
@JsonIgnoreProperties(ignoreUnknown = true)public class ActivityLocation {
@JsonProperty("Address")
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
地址.java
@JsonIgnoreProperties(ignoreUnknown = true)public class Address {
@JsonProperty("City")
private String city;
@JsonProperty("StateProvinceCode")
private String stateProvinceCode;
@JsonProperty("CountryCode")
private String countryCode;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
public String getStateProvinceCode() {
return stateProvinceCode;
}
public void setStateProvinceCode(String stateProvinceCode) {
this.stateProvinceCode = stateProvinceCode;
}
}
这是我可以正确映射json的代码:
public static void main(String[] args) { String jsonMessage = "" +
"{" +
" \"Activity\": [{ " +
" \"ActivityLocation\": { " +
" \"Address\": { " +
" \"City\": \"Hana\", " +
" \"StateProvinceCode\": \"Hi\", " +
" \"CountryCode\": \"US\" " +
" } " +
" } " +
" }, " +
" { " +
" \"ActivityLocation\": { " +
" \"Address\": { " +
" \"City\": \"Honolulu\", " +
" \"StateProvinceCode\": \"Hi\", " +
" \"CountryCode\": \"US\" " +
" } " +
" } " +
" }] " +
"} ";
try {
ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
Shipment shipment = mapper.readValue(jsonMessage, Shipment.class);
System.out.println("shipment.toString = " + shipment.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
调整jsonMessage
var中的数据时,遇到了上面提到的错误:
"{" + " \"Activity\": [{ " +
" \"ActivityLocation\": { " +
" \"Address\": { " +
" \"City\": \"Hana\", " +
" \"StateProvinceCode\": \"Hi\", " +
" \"CountryCode\": \"US\" " +
" } " +
" } " +
" }, " +
" { " +
" \"ActivityLocation\": { " +
" \"Address\": \"\" " +
" } " +
" } " +
" }] " +
"} ";
因此,从此更改json时会发生问题:
{ "ActivityLocation": {
"Address": {
"City": "Honolulu",
"StateProvinceCode": "Hi",
"CountryCode": "US"
}
}
}]
对此:
{"ActivityLocation": {
"Address": ""
}
}
我没有为我的Address
bean 发送值,而是得到一个空字符串。不幸的是,我从第三方接收数据,但无法控制我收到的数据。
是否需要添加注释才能处理此问题?
回答:
尝试设定
mapper.configure(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT,
true)
要么
mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
取决于您的Jackson版本。
以上是 没有要从字符串值('')反序列化的字符串参数构造函数/工厂方法 的全部内容, 来源链接: utcz.com/qa/407491.html