渲染为JSON时Grails中缺少方法异常

时间:2015-05-14 15:26:52

标签: json exception grails methods groovy

我完全是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,这是我能想到的唯一问题。

1 个答案:

答案 0 :(得分:0)

您缺少new个关键字。应该是

def newItem = new Item(request.JSON)