Grails MongoDB集合访问

Grails MongoDB集合访问,grails,Grails,我将MongoDB专门用于Grails REST应用程序和如下所示的域。此响应呼叫失败: @Secured(['permitAll']) def index() { respond Person.list() } 带着错误 ERROR errors.GrailsExceptionResolver - IllegalAccessException occurred when processing request: [GET] /teesheet/person Class org.cod

我将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
作品

假设您使用的是插件,那么域类需要@Entity

import grails.persistence.Entity

@Entity
class Person {
  static mapWith = "mongo"

  String name
  String firstName
  String lastName
  String email

}

我添加了mapWith=mongo,因为我不确定您是否在使用mongo插件的同时使用hibernate插件;如果没有,请删除hibernate,否则,它可能会

我目前正在使用低级调用来使用游标进行迭代,但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

我没有使用hibernate插件或任何其他数据存储,只是使用了Mongo3.0.2插件和Grails2.4.4的MongoDB。我添加了@Entity和关联的导入,错误得到了解决,但respond Person.list返回mongo游标,而不是集合:{cursor:{class:com.mongodb.DBCursor,集合:{DB:{authenticated:false,…只需迭代光标,将Person对象放在列表中,返回列表,尽管这通常是使用较低级别驱动程序时所必需的。每当我更改映射时,我都会物理删除存储二进制文件的本地目录,然后刷新/重建依赖项;乱扔垃圾会导致问题,请尝试:-@Entity是Mongo域类或任何其他类中都不需要该注释。该注释是Grails在编译过程中通过AST转换添加的-无需手动执行该操作。@BurtBeckwith忽略该标记对我来说只不过是个问题,它似乎也解决了OP的问题,并没有说你错了,只是对经验进行了评论。so记录在哪里?
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