在Grails中渲染具有自定义mime类型的视图 [英] Rendering a view with custom mime type in Grails

查看:47
本文介绍了在Grails中渲染具有自定义mime类型的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am attempting to render text for reponse in Grails with a custom contentType. My desired contentType is: application/vnd.api+json

我正在测试以下内容

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

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

which does not render throwing an exception that message is a missing property.

以下方法可以正常工作:

While the following works fine:

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

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

My Config.groovy has the following under json mime.type:

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

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

My question, how to render with a custom mime-type in Grails?

推荐答案

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

Do you have the accept header in the request set the custom content-type?

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

The accept header is one way for client to inform server which content-type would be acceptable for itself.

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

In config.groovy the below setting has to be set as well to use accept headers

grails.mime.use.accept.header = true

我还将尝试以常规方式呈现响应:

I would also try rendering the response in the conventional way:

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

这篇关于在Grails中渲染具有自定义mime类型的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆