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

问题描述 投票:1回答:1

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

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

我的Controller类如下

class SchemaImporterController {
    static responseFormats = ['json']

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

我的GSON索引视图如下

model{
    List<String> stringList
}

json{
    informationList stringList.each { String str ->
        singleEntry str
    }
}

在声明模型变量时,我也尝试了不同的变体,如List stringList,ArrayList

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

gson grails-controller grails3
1个回答
1
投票

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

model{
    List<String> stringList
}

json{
    informationList stringList, { String str ->
        singleEntry str
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.