呈现为 JSON 时 Grails 中缺少方法异常

Missing Method Exception in Grails when rendering as JSON

我是 Grails 的新手,正在尝试使用我正在创建的待办事项应用程序来实现它。我的第一个控制器函数 add() 有问题。这是我的代码:

package server

import grails.converters.JSON

class TodoListController {

    def addItem() {
        def newItem = Item(request.JSON)
        newItem.save(flush: true)
        render newItem as JSON
    }
}


// These are separated into two file, combined for convenience in this pastebin
//----------------------------------

//Item domain class

package server

class Item {

    String username
    String itemName
    String priority
    String itemType
    boolean completed

    static constraints = {
        username nullable: false, size: 3..20
        itemName nullable: false
        priority nullable: true
        itemType nullable: true
    }
}

(我在 pastebin 中合并了两个文件——它们在项目中是分开的)

当尝试访问我的 add() 地址时,我收到此错误:

Error |
2015-05-14 11:15:14,658 [http-bio-8080-exec-2] ERROR errors.GrailsExceptionResolver  - MissingMethodException occurred when processing request: [GET] /server/todoList/addItem
No signature of method: server.TodoListController.Item() is applicable for argument types: (org.codehaus.groovy.grails.web.json.JSONObject) values: [[:]]
Possible solutions: addItem(), grep(), use([Ljava.lang.Object;), grep(java.lang.Object), wait(), dump(). Stacktrace follows:
Message: No signature of method: server.TodoListController.Item() is applicable for argument types: (org.codehaus.groovy.grails.web.json.JSONObject) values: [[:]]
Possible solutions: addItem(), grep(), use([Ljava.lang.Object;), grep(java.lang.Object), wait(), dump()
    Line | Method
->>    8 | addItem   in server.TodoListController
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    198 | doFilter  in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter  in grails.plugin.cache.web.filter.AbstractFilter
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run       in java.lang.Thread

我不知道哪里出了问题。我包括 converters.json,这是我能想到的唯一问题。

您缺少 new 关键字。应该是

def newItem = new Item(request.JSON)