【问题标题】:Rendering a view with custom mime type in Grails在 Grails 中使用自定义 mime 类型渲染视图
【发布时间】:2013-06-15 23:02:00
【问题描述】:

我正在尝试使用自定义 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-type 进行渲染?

【问题讨论】:

    标签: grails mime-types content-type


    【解决方案1】:

    请求中是否有accept 标头设置自定义内容类型?

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

    config.groovy 中,还必须设置以下设置才能使用接受标头

    grails.mime.use.accept.header = true

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

    render(contentType: "application/vnd.api+json", text: [message: 'some text', foo: 'bar'])
    

    【讨论】:

    • accept.header 是假的,启用它没有帮助!我仍然收到异常“消息是缺少的属性”
    • 这渲染得很好: With text/json pastebin.com/0kPhTFv8 This with application/vnd.api+json pastebin.com/PL0F7Pcj 最终出错
    • @Armand 你能试试这种渲染方式吗render(contentType: "application/vnd.api+json", text: [message: 'some text', foo: 'bar'])
    • 这成功了!谢谢!我相信我在将 accept.header 更改为 true 之前尝试过这个!
    猜你喜欢
    • 1970-01-01
    • 2010-09-22
    • 2012-01-31
    • 1970-01-01
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多