我有以下 Controller 代码:
def save(MyModel model) {
model.save()
}
我正在使用以下方法对其进行测试:
//e.g. 2ff59e55-ee3d-4f66-8bfa-00f355f52c49
def uuid = UUID.randomUUID.toString()
controller.request.contentType = JSON_CONTENT_TYPE
controller.request.method = 'POST'
controller.request.json = "{'uuid': '$uuid', 'description': 'test object', 'count': 1}"
controller.save()
但是,每次我运行测试时,
org.apache.commons.lang.UnhandledException:
org.codehaus.groovy.grails.web.converters.exceptions.ConverterException:
org.codehaus.groovy.grails.web.json.JSONException: Value out of sequence: expected mode to be
OBJECT or ARRAY when writing '{'uuid': '2ff59e55-ee3d-4f66-8bfa-00f355f52c49', 'description': 'test object', 'count': 1}' but was INIT
最佳答案
JSON 转换器在 Groovy 字符串上阻塞。我通过拍.toString()
解决了这个问题最后:"{'uuid':'$uuid'}".toString()
.
关于json - Grails - JSON 绑定(bind)导致 JSONException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24040673/