gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 16:01:30 24 4
gpt4 key购买 nike

我完全不熟悉Grails,并尝试使用我正在创建的todo应用程序来实现它。我的第一个 Controller 功能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)

关于json - 呈现为JSON时Grails中缺少方法异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30241182/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com