Keine String-argument-Konstruktor/factory-Methode zum Deserialisieren von der String-Wert (")
Ich in ein json-parsing-Problem bei der Verwendung der ObjectMapper
Klasse aus der com.fasterxml.jackson.databind
Paket, und die Fehler, die ich bekomme ist:
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-Anwendung, wo dieses problem Auftritt, wird eine Spring-MVC-Anwendung mit einem AngularJS Frontend, aber ich kann das duplizieren der Ausgabe mit einem viel kleineren, alle java-Programm. Hier sind meine Bohnen:
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;
}
}
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;
}
}
Hier ist der code, wo ich richtig anzeigen 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();
}
}
Beim einstellen der Daten in die jsonMessage
var ist wenn ich die Fehler, die ich oben erwähnt habe:
"{" +
" \"Activity\": [{ " +
" \"ActivityLocation\": { " +
" \"Address\": { " +
" \"City\": \"Hana\", " +
" \"StateProvinceCode\": \"Hi\", " +
" \"CountryCode\": \"US\" " +
" } " +
" } " +
" }, " +
" { " +
" \"ActivityLocation\": { " +
" \"Address\": \"\" " +
" } " +
" } " +
" }] " +
"} ";
So, das problem tritt beim ändern des json-von daher:
{
"ActivityLocation": {
"Address": {
"City": "Honolulu",
"StateProvinceCode": "Hi",
"CountryCode": "US"
}
}
}]
zu diesem:
{
"ActivityLocation": {
"Address": ""
}
}
Anstatt Werte für meine Address
bean, ich bin immer nur einen leeren string. Leider bin ich mit dem Empfang meiner Daten aus Dritter und haben keine Kontrolle über die Daten, die ich erhalten.
Gibt es eine Anmerkung, die Hinzugefügt werden muss, um in der Lage sein, dies zu behandeln?
Du musst angemeldet sein, um einen Kommentar abzugeben.
Versuchen Sie
mapper.configure(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true)
oder
je nach Jackson version.
Hatte, als ich versehentlich Berufung war
statt
Also, stellen Sie sicher, dass Sie anrufen, die richtige Methode, da argument gleich sind und IDE können viele Dinge finden
Mein code funktioniert auch so wie die Antwort oben.
Der Grund dafür ist, dass die json-von jackson ist, anders mit dem json gesendet vom controller.
Und json ist wie(die deserialisiert werden, normalerweise):
aber die json senden aus dem anderen Dienst, wie zum Beispiel:
Können Sie den Unterschied bemerken, wenn Sie sich mit den param-ohne initiation.
Seien Sie vorsichtig,