用Jackson反序列化:“org.codehaus.jackson.map.JsonMappingException:无法反序列化[projectname]的实例。”

时间:2014-01-24 10:46:43

标签: java javascript jersey sencha-touch-2 jackson

我正在开发一个基于this tutorial RESTful 应用程序(这只是一个关于笔记的简单网络应用程序,默认情况下没有任何网络服务)。

不幸的是,当我尝试添加注释时,第一次没有错误,并且注释实际上保存在单例服务器端的arrayList中。没有错误......

但是当我重新添加其他注释时,它在我的jetty服务器控制台中失败并出现此错误:

janv. 24, 2014 7:54:29 AM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
Grave: The exception contained within MappableContainerException could not be mapped to a response, re-throwing to the HTTP container
org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of [...].noteapp.rest.model.Note out of START_ARRAY token
at [Source: org.eclipse.jetty.server.HttpInput@71453e59; line: 1, column: 1]
at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:219)

奇怪的是,如果我重新加载页面,它会再次运行一次。


以下是我的笔记(model.Note.js)的Sencha 模型

Ext.define("NotesApp.model.Note", {
    extend: "Ext.data.Model",
    config: {
        idProperty: 'id',
        fields: [
            { name: 'id', type: 'string' },
            { name: 'dateCreated', type: 'date', dateFormat: 'U' },
            { name: 'title', type: 'string' },
            { name: 'narrative', type: 'string' }
        ],
        validations: [
            { type: 'presence', field: 'id' },
            { type: 'presence', field: 'dateCreated' },
            { type: 'presence', field: 'title', message: 'Veuillez entrer un titre pour cette note' }
        ]
    }
});

以下是存储空间(store.Notes.js):

Ext.define("NotesApp.store.Notes", {
    extend: "Ext.data.Store",
    config: {
        autoLoad: true,
        model: "NotesApp.model.Note",
        proxy: {
            type: 'ajax',
            url: '/restnotes/notearray',
            reader: {
                type: 'json',
                rootProperty: 'note'
            },
            writer: {
                type: 'json',
                writeAllFields: true
            },
            actionMethods: {
                read: "GET",
                create: "POST",
                update: "POST",
                destroy: "DELETE"
            },
            api: {
                read: 'restnotes/notearray/arraynotes',
                create: 'restnotes/notearray/note',
                update: 'restnotes/notearray/note',
                destroy: 'restnotes/notearray/note'
            },
            id: 'notes-app-store'
        },
        sorters: [
            { property: 'dateCreated', direction: 'DESC'}
        ],
        grouper: {
            sortProperty: "dateCreated",
            direction: "DESC",
            groupFn: function (record) {

                if (record && record.data.dateCreated) {
                    return record.data.dateCreated.toDateString();
                } else {
                    return '';
                }
            }
        }
    }
});

以下是 ctrl 的一部分,我在其中添加了一个注释(controller.Notes.js):

onNewNoteCommand: function () {

    console.log("onNewNoteCommand");

    var now = new Date();
    var noteId = (now.getTime()).toString() + (this.getRandomInt(0, 100)).toString();


    var newNote = Ext.create("NotesApp.model.Note", {
        id: noteId,
        dateCreated: parseInt(now.getTime()/1000),
        title: "",
        narrative: ""
    });

    this.activateNoteEditor(newNote);

},

[...]

onSaveNoteCommand: function () {

    console.log("onSaveNoteCommand");

    var noteEditor = this.getNoteEditor();

    var currentNote = noteEditor.getRecord();
    var newValues = noteEditor.getValues();

    // Update the current note's fields with form values.
    currentNote.set("title", newValues.title);
    currentNote.set("narrative", newValues.narrative);

    var errors = currentNote.validate();

    if (!errors.isValid()) {
        Ext.Msg.alert('Wait!', errors.getByField("title")[0].getMessage(), Ext.emptyFn);
        currentNote.reject();
        return;
    }

    var notesStore = Ext.getStore("Notes");

    if (null == notesStore.findRecord('id', currentNote.data.id)) {
        notesStore.add(currentNote);
    }

    notesStore.sync();

    notesStore.sort([
        { property: 'dateCreated', direction: 'DESC'}
    ]);

    this.activateNotesList();
},

这是我的 java bean (Note.java):

@JsonAutoDetect
@JsonDeserialize()
public class Note {

    private String id;
    private long dateCreated;
    private String title;
    private String narrative;


    @JsonCreator
    public Note(@JsonProperty("id") String id, @JsonProperty("dateCreated") Date dateCreated, @JsonProperty("title") String title, @JsonProperty("narrative") String narrative) {
        System.out.println("Il passe dans le constructeur JSON lors de la création dans sencha");
        this.id = id;
        this.dateCreated = dateCreated.getTime();
        this.title = title;
        this.narrative = narrative;
        System.out.println(this.toString());
    }

    public Note(String id, long dateCreated, String title, String narrative) {
        System.out.println("Il passe dans le constructeur normal");
        this.id = id;
        this.dateCreated = dateCreated;
        this.title = title;
        this.narrative = narrative;
        System.out.println(this.toString());
    }
    [...all getter-setters and else...]

}

我没有给服务器的控制器添加JSON中的注释发送到arraylist,因为错误不存在(我调试了它,错误似乎是客户端)

我搜索了很多关于这个错误的时间,我尝试了很多解决方案,但没有任何效果。

我希望你能帮助我。非常感谢你。

1 个答案:

答案 0 :(得分:0)

我终于找到了问题。 当我sync()我的存储空间时,它会发送所有内容,因为我没有将旧注释设置为currentNote.dirty = false。它在我的 controller.Notes.js 中,所以在我同步后,我设置currentNote.dirty = false

notesStore.sync();

currentNote.dirty = false;

//Tri des notes
notesStore.sort([
    { property: 'dateCreated', direction: 'DESC'}
]);

this.activateNotesList();

然后它起作用,因为Sencha每次只发送一个对象。

相关问题