没有从字符串值('')反序列化的字符串参数构造函数/工厂方法

新手上路,请多包涵

我在使用 com.fasterxml.jackson.databind 包中的 ObjectMapper 类时遇到了 json 解析问题,我得到的错误是:

 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 程序复制该问题。这是我的豆子:

装运.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;
    }
}

活动.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;
    }
}

活动位置.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 发送值,而是得到一个空字符串。不幸的是,我从第三方接收我的数据并且无法控制我收到的数据。

是否需要添加注释才能处理此问题?

原文由 Stephen 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.5k
2 个回答

尝试设置

mapper.configure(
          DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT,
          true)

或者

mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);

取决于您的杰克逊版本。

原文由 Abhijit Sarkar 发布,翻译遵循 CC BY-SA 4.0 许可协议

当我不小心打电话时有这个

mapper.convertValue(...)

代替

mapper.readValue(...)

所以,只要确保你调用了正确的方法,因为参数是相同的并且 IDE 可以找到很多东西

原文由 Andrii Plotnikov 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题