Rendering 'as JSON' in Grails with conditional operator doesn't render correctly

前端 未结 4 721
不知归路
不知归路 2020-12-11 06:27

Came across this strange result today trying to render a list of objects as JSON in Grails 2.0.4...(i know i\'m gonna regret asking this on account of something right under

相关标签:
4条回答
  • 2020-12-11 06:44

    What you do is calling render with the parameters in ( ), and then applying "as JSON" to the result!

    Don't forget that leaving the parentheses out is just a shortcut for a method call, but the same rules still apply.

    0 讨论(0)
  • 2020-12-11 06:53

    This is a normal behavior of render. When you provide arguments to render without braces like

    render model as JSON

    It makes an implicit adjustment setting up the content-type to text/json. But in the later case, you have unknowingly made the render to use the braces like [mark on the first brace after render makes render use the normal render()]

    render ((products) ? [products:products] : [products:"No products found"]) as JSON.

    In the above case, you have to pass in the named parameters to render mentioning the contentType, text or model, status etc. So in order to render the inline control logic as JSON in browser/view you have to do like below:

    render(contentType: "application/json", text: [products: (products ?: "No products found")] as JSON)
    

    You can also use content-type as text/json. I prefer application/json.

    UPDATE
    Alternative Simplest Way:
    render([products: (products ?: "No products found")] as JSON)

    0 讨论(0)
  • 2020-12-11 06:53

    The essence of your problem here is that the groovy compiler interprets

    render x as JSON
    

    to mean

    render (x as JSON)
    

    but it interprets

    render (x) as JSON
    

    to mean

    (render x) as JSON
    

    If a method name (in this case render) is followed immediately by an opening parenthesis, then only code up to the matching closing parenthesis is considered to be the argument list. This is why you need an extra set of parentheses to say

    render ((x) as JSON)
    
    0 讨论(0)
  • 2020-12-11 06:58

    Don't know the reason. Try to use like this:

    render(contentType: 'text/json') {[
        'products': products ? : "No products found"
    ]}
    
    0 讨论(0)
提交回复
热议问题