简体   繁体   English

在Grails中使用HTTP状态代码呈现JSON的简便方法

[英]Easy way to render JSON with HTTP status code in Grails

Is there a shorthand way to do this without explicit "text/json" designation? 没有明确的"text/json"指定,是否有一种速记方式可以做到这一点?

def remoteError = {     
  render( status: 500, contentType: "text/json"){
      error( exception: "a remote exception occurred")
  }
}

I tried using as JSON ...no content is returned but the status code is correct... 我尝试使用as JSON ...没有返回内容,但状态代码是正确的...

render( status: 500, exception: params.exception) as JSON 

If you use a converter parameter to render then you cannot specify any other parameter such as status like you normally would when using gsp views. 如果使用转换器参数进行渲染,则无法像使用gsp视图时通常那样指定任何其他参数,例如状态。 You can however set the response status prior to calling render: 但是,您可以在调用render之前设置响应状态:

response.status = 500
render([error: 'an error occurred'] as JSON)
render(status:500,text:(errors as JSON).toString(),contentType: 'application/json')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM