在Grails中使用带有rest配置文件的gson时,String列表抛出ClassCastException

时间:2018-01-03 06:45:14

标签: gson grails-controller grails3

当我尝试使用GSON和Grails Rest配置文件应用程序渲染字符串列表时,我正在

composer clear-cache

我的控制器类如下

java.lang.ClassCastException: _info_app_name__schemaImporter_index_gson$_run_closure1 cannot be cast to grails.plugin.json.builder.StreamingJsonBuilder$StreamingJsonDelegate

我的GSON索引视图如下

class SchemaImporterController {
    static responseFormats = ['json']

    def index() {
        def data = [:]
        data.stringList = [
            'One',
            'Two',
            'Three',
            'Four
        ] as ArrayList<String>
        return data
    }
}

在声明模型变量时,我也尝试了不同的变体,     列出stringList,     数组列表

但每次我这样做都会返回相同的错误。知道为什么吗?这里要注意的是,当我渲染其他域类时,它的工作正常。

1 个答案:

答案 0 :(得分:1)

我找到了罪魁祸首。迭代列表时我正在使用每个。我删除了它,并使我的GSON视图如下,它开始工作正常。

model{
    List<String> stringList
}

json{
    informationList stringList, { String str ->
        singleEntry str
    }
}