. Advertisement .
..3..
. Advertisement .
..4..
In the process of completing my work, I encountered the following problem:
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 ('')
Below is the code I ran:
@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;
}
}
@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;
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
public class ActivityLocation {
@JsonProperty("Address")
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
@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;
}
}
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();
}
}
"{" +
" "Activity": [{ " +
" "ActivityLocation": { " +
" "Address": { " +
" "City": "Hana", " +
" "StateProvinceCode": "Hi", " +
" "CountryCode": "US" " +
" } " +
" } " +
" }, " +
" { " +
" "ActivityLocation": { " +
" "Address": "" " +
" } " +
" } " +
" }] " +
"} ";
{
"ActivityLocation": {
"Address": {
"City": "Honolulu",
"StateProvinceCode": "Hi",
"CountryCode": "US"
}
}
}]
{
"ActivityLocation": {
"Address": ""
}
}
What’s causing it, and how can it be resolved in the “no string argument constructor factory method to deserialize from string value“ in the java?
The cause: This error occured because the object “Address” is being deserialized from the string “” \ “” instead of an object description as “{…}”, so the deserializer couldn’t find a method initialization address with String argument.
Solution: To avoid this error, “” need to be replaced by {} .
This was the moment I accidentally called.
Instead of
You should call the correct method since argument and IDE can find many different things.