2015-01-15 93 views
1

我最近從2.1.0升級項目中的Grails 2.4.4Grails的bindData不升級後嵌套對象相匹配2.4.4

在不同的地方,我們使用bindData映射傳入的JSON域對象包括嵌套的對象。這在升級或符號更改後似乎不起作用。

例如

Class Book { 
    Author author 
    ... 
} 

在JSON

{ "author.id" : 7 } 

傳遞和調用

def book = new Book() 
bindData(book, request.JSON, [include: ['author']]) 

會找到以前筆者7,重視他的例子書。現在它不綁定任何東西。有沒有其他人遇到這個問題或類似的東西?

感謝

編輯:

更新傳入的JSON修復了這個問題,是一個潛在的解決方法:

{ "author" : { "id" : 7 } } 

這種格式會更有意義,但遺憾的是它沒有內置這種方式首先。

+0

同樣重要的是向我們展示圖書的看法了。 – dsharew

回答

1

這可能對你有幫助。
出於某種原因,我不能使用默認的數據綁定器,所以我想出了這個自定義數據綁定器。現在

Object bindDataCustom (Object targetObject, Map sourceObject) { //named bindDataCustom so it cant confuse with bindData 
     sourceObject.each{ property, value-> 
      if(property.toString().contains(".")){ 
       String[] nestedProperty = property.toString().split("\\."); //e.g split author.id to author and id 
       String subObjectName = nestedProperty[0][0].toUpperCase() + nestedProperty[0].substring(1) 
       String subObjectPropertyName = nestedProperty[1] 

       Object subObject = Class.forName(packageName + subObjectName, true, Thread.currentThread().getContextClassLoader())."findBy${subObjectPropertyName[0].toUpperCase()}${subObjectPropertyName.substring(1)}"(value) 

       targetObject."${nestedProperty[0]}" = subObject 

      }else{ 
       DataBindingUtils.bindObjectToInstance(targetObject, sourceObject, [property], [], null) 
      } 
     } 

     return targetObject; 
    } 

,你可以這樣做:

def book = new Book() 
bindDataCustom(book, request.JSON) 
+0

如果我不能以舊的方式運作,這是一個潛在的解決方法。謝謝。 –