2

我正在尝试使用自定义 contentType 在 Grails 中呈现响应文本。我想要的 contentType 是:application/vnd.api+json

我正在测试以下内容

render(contentType: "application/vnd.api+json") {
    message = 'some text'
    foo = 'bar'
}

这不会引发异常,即消息是缺少的属性。

虽然以下工作正常:

render(contentType: "text/json") {
    message = 'some text'
    foo = 'bar'
}

我的 Config.groovy 在 json mime.type 下有以下内容:

grails.mime.types = [
...
    json:          [
        'application/json',
        'text/json',
        'application/vnd.api+json'
    ],
...
]

我的问题,如何在 Grails 中使用自定义 mime 类型进行渲染?

4

1 回答 1

4

您是否accept将请求中的标头设置为自定义内容类型?

接受标头是客户端通知服务器自己可以接受哪种内容类型的一种方式。

config.groovy下面的设置中也必须设置使用接受标头

grails.mime.use.accept.header = true

我也会尝试以传统方式呈现响应:

render(contentType: "application/vnd.api+json", text: [message: 'some text', foo: 'bar'])
于 2013-06-19T13:24:48.370 回答