2015-05-14 2 views
0

Я новичок в Grails и пытаюсь реализовать его с помощью приложения todo, которое я создаю. У меня возникают проблемы с моей первой функцией контроллера add(). Вот мой код:Исчезновение отсутствующего метода в Grails при рендеринге как JSON

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- они разделены в проекте)

При попытке получить доступ к моей добавить() адрес, я получаю эту ошибку:

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, и это единственная проблема, о которой я могу думать.

ответ

0

Вам не хватает ключевое слово new. Должно быть

def newItem = new Item(request.JSON) 
+0

Спасибо! Отлично работает – Ajv2324