Wie Konfiguriere ich den die jackson-objectmapper korrekt Deserialisieren zu pojo?

Bin ich ein bisschen ein problem zu verstehen, wie sollte ich konfigurieren der objectMapper und pojo, wenn Deserialisieren. Meine Json wird von einer anderen Anwendung erstellt, die
unterstützt werden sowohl xml als auch json. Gibt es eine Liste mit myobject, aber das Json enthält die Art, wie diese:

[
    {
        "myobject": {
            "somethingcool": "amazing",
            "contactPersonsForMyObject": [
                "[email protected]",
                "[email protected]"
            ],
            "myObjectId": "c85e48730501bfae41e67714c6131b7d"
        }
    },
    {
        "myobject": {
            "somethingcool": "cool",
            "contactPersonsForMyObject": [
                "[email protected]",
                "[email protected]"
            ],
            "myObjectId": "c85e48730501bfae41e67714cqwerty"
        }
    }
]

Meiner Klasse:

public class MyObject {

    private String myObjectId;
    private String somethingcool;
    private List<String> contactPersonsForMyObject;

    public String getMyObjectId() {
        return myObjectId;
    }

    public void setMyObjectId(String myObjectId) {
        this.myObjectId = myObjectId;
    }

    public String getSomethingcool() {
        return somethingcool;
    }

    public void setSomethingcool(String somethingcool) {
        this.somethingcool = somethingcool;
    }

    public List<String> getContactPersonsForMyObject() {
        return contactPersonsForMyObject;
    }

    public void setContactPersonsForMyObject(List<String> contactPersonsForMyObject) {
        this.contactPersonsForMyObject = contactPersonsForMyObject;
    }
}

Aber wenn dabei:

List<MyObject> myObjects = mapper.convertValue(rootNode, new TypeReference<List<MyObject>>() {});

Ich bin immer eine Ausnahme, die besagt:

java.lang.IllegalArgumentException: Unrecognized field "myobject" (Class com.domain.MyObject), not marked as ignorable
 at [Source: N/A; line: -1, column: -1] (through reference chain: com.domain.MyObject["myobject"])

Es ist wie der mapper nicht verstehen, die zusätzliche "Schicht".
Beim serialisieren um diese Struktur ist es möglich, konfigurieren Sie den mapper wie diese: mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);

Also sollte es irgendwie zu tun das Gegenteil?

Danke!

InformationsquelleAutor jakob | 2011-08-31
Schreibe einen Kommentar