Kein String-Argument-Konstruktor/Factory-Methode zum Deserialisieren aus dem String-Wert ('')Java

Java-Forum
Anonymous
 Kein String-Argument-Konstruktor/Factory-Methode zum Deserialisieren aus dem String-Wert ('')

Post by Anonymous »

Bei der Verwendung der ObjectMapper-Klasse aus dem com.fasterxml.jackson.databind-Paket tritt ein JSON-Parsing-Problem auf, und die Fehlermeldung lautet:

Code: Select all

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 ('')
Die Webanwendung, bei der dieses Problem auftritt, ist eine Spring MVC-Anwendung, die ein AngularJS-Frontend verwendet, aber ich kann das Problem mit einem viel kleineren reinen Java-Programm reproduzieren. Hier sind meine Bohnen:

Shipment.java

Code: Select all

@JsonIgnoreProperties(ignoreUnknown = true)
public class Shipment {
@JsonProperty("Activity")
private ArrayList activity;
public ArrayList getActivity() {
return activity;
}
public void setActivity(ArrayList activity) {
this.activity = activity;
}
}
Activity.java

Code: Select all

@JsonIgnoreProperties(ignoreUnknown = true)
public class Activity {
@JsonProperty("ActivityLocation")
private ArrayList activityLocation;
public ArrayList getActivityLocation() {
return activityLocation;
}
public void setActivityLocation(ArrayList  activityLocation) {
this.activityLocation = activityLocation;
}
}
ActivityLocation.java

Code: Select all

@JsonIgnoreProperties(ignoreUnknown = true)
public class ActivityLocation {
@JsonProperty("Address")
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
Address.java

Code: Select all

@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;
}
}
Hier ist der Code, mit dem ich den JSON richtig zuordnen kann:

Code: Select all

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();
}
}
Beim Anpassen der Daten in der jsonMessage-Variable tritt der oben erwähnte Fehler auf:

Code: Select all

    "{" +
"   \"Activity\": [{ " +
"       \"ActivityLocation\": { " +
"           \"Address\": { " +
"               \"City\": \"Hana\", " +
"               \"StateProvinceCode\": \"Hi\", " +
"               \"CountryCode\": \"US\" " +
"           } " +
"       } " +
"   }, " +
"   { " +
"       \"ActivityLocation\": { " +
"           \"Address\": \"\" " +
"           } " +
"       } " +
"   }] " +
"} ";
Das Problem tritt also auf, wenn der JSON von hier aus geändert wird:

Code: Select all

{
"ActivityLocation": {
"Address": {
"City": "Honolulu",
"StateProvinceCode": "Hi",
"CountryCode": "US"
}
}
}]
dazu:

Code: Select all

{
"ActivityLocation": {
"Address": ""
}
}
Anstatt Werte für meine Address-Bean zu senden, erhalte ich nur eine leere Zeichenfolge. Leider erhalte ich meine Daten von einem Dritten und habe keine Kontrolle über die Daten, die ich erhalte.

Muss eine Anmerkung hinzugefügt werden, um damit umgehen zu können?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post