如何在Grails中使用正确的内容类型呈现任意JSON?

时间:2016-03-16 22:34:22

标签: json grails

我试图输出像{"error": "endpoint not available"}

这样的简单内容

下面的代码段使用内容类型 text / html

进行渲染
render ([error: 'endpoint not available']) as JSON

我也尝试过:

render(contentType: 'application/json') {
  error {
    "endpoint not available"
  }
}

我知道它并没有产生所需的JSON。

如何使用 application / json 内容类型生成我想要的JSON?

1 个答案:

答案 0 :(得分:0)

尝试:

def responseData = [
    'error': 'endpoint not available'
]
render responseData as JSON
相关问题