MongoDB Collection Access

时间:2014-12-21 07:13:06

标签: grails

我将MongoDB专门用于Grails REST应用程序和下面显示的域。此回复呼叫失败:

@Secured(['permitAll'])
def index() {
    respond Person.list()
}

错误

ERROR errors.GrailsExceptionResolver  - IllegalAccessException occurred when processing request: [GET] /teesheet/person
Class org.codehaus.groovy.grails.web.converters.marshaller.json.GroovyBeanMarshaller can not access a member of class java.util.Collections$UnmodifiableCollection with modifiers "public". Stacktrace follows:
Message: Class org.codehaus.groovy.grails.web.converters.marshaller.json.GroovyBeanMarshaller can not access a member of class java.util.Collections$UnmodifiableCollection with modifiers "public"

尝试将集合转换为JSON也会失败并出现相同的错误。

def personList = Person.list() as JSON

low level API有效。

package com.tworks.teesheet

import grails.rest.Resource

class Person {
  String name
  String firstName
  String lastName
  String email

  User userPerson

  TimeZone timeZone = TimeZone.getTimeZone("America/Los_Angeles")
  Date dateCreated = new Date()
  Date dateModified = new Date()
}

2 个答案:

答案 0 :(得分:0)

假设您正在使用Mongo for Grails插件,域名类需要@Entity ...

import grails.persistence.Entity

@Entity
class Person {
  static mapWith = "mongo"

  String name
  String firstName
  String lastName
  String email

}

我添加了mapWith="mongo",因为我不确定你是否在mongo插件旁边使用了hibernate插件;如果您不是,请删除休眠,否则可能interfere

答案 1 :(得分:0)

我目前正在使用低级调用来使用游标进行迭代,但似乎respond Person.list()应该可以正常工作。这段代码正在运行:

def cursor = Person.collection.find()
def items = []
try {
    while (cursor.hasNext()) {
        items << com.mongodb.util.JSON.serialize(cursor.next())
    }
} finally {
    cursor.close()
}
log.info("items: ${items}")
render items