2015-04-07 45 views
0

如何在呈現的JSON中包含errors.allErrors?JSON中的Grails allErrors

我想我的JSON看起來是這樣的:

{id:10 
name:"" 
    _errors: [{name:"Name cannot be blank"}], 
    children: [{field:"value", _errors:[]}, ...] 
} 

這樣使用像角的時候,我們在同一個「階級」爲領域中的錯誤。當你有一個複雜的樹時,在另一個地圖上返回錯誤不起作用。

回答

0

這是當你放棄尋找答案。

一如既往的Grails已經它覆蓋有Marshallers插件(https://grails.org/plugin/marshallers

添加在插件然後加入以下域類

static marshalling={ 
    json{ 
     angular{  
      virtual { 
       _errors {value, json ->      
        json.value (com.ocom.grails.ErrorsMap.generateMap(value));   
       } 
      } 
     } 
    } 
} 

凡com.ocom.grails.ErrorsMap.generateMap (值)將採取域類並返回一個錯誤數組

在控制器中返回像這樣的JSON

render JSON.use('angular') {responseJson as JSON} 

This Grails 2.3.11

0

變得相當JSON Grails中最簡單的方法是從地圖,所以:

[id:10, 
name:"", 
_errors: [name:"Name cannot be blank"], 
children: [field:"value", _errors:[], ...] 
] as grails.converters.JSON 
+1

你看過這個問題嗎? – dsharew