升级到2.4.4后,Grails bindData与嵌套对象不匹配 [英] Grails bindData doesn't match nested objects after upgrade to 2.4.4

查看:186
本文介绍了升级到2.4.4后,Grails bindData与嵌套对象不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近将一个项目从2.1.0升级到了Grails 2.4.4在很多地方,我们使用bindData将传入的JSON映射到域对象,包括嵌套对象。这在升级或符号更改后似乎不起作用。



例如

 课本{
作者作者
...
}

传入JSON

  {author.id:7} 

然后调用

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

以前会找到作者7并将他附加到示例书。现在它不绑定任何东西。有没有人遇到这个问题或类似的东西?



谢谢

编辑:



更新传入的JSON修复了这个问题,并且是一个潜在的解决方法:

  {author :{id:7}} 

这种格式更有意义,但不幸的是它不是建立这种方式。

解决方案

这可能会帮助你。

出于某种原因,我不能使用默认的数据绑定器,这样我就可以使用这个自定义数据绑定器


  Object bindDataCustom(Object targetObject,Map sourceObject){//命名为bindDataCustom,所以它无法与bindData 
sourceObject混淆。每个{属性,值 - >
if(property.toString()。contains(。)){
String [] nestedProperty = property.toString()。split(\\\。); //例如split author.id作者和id
字符串subObjectName = nestedProperty [0] [0] .toUpperCase()+ nestedProperty [0] .substring(1)
字符串subObjectPropertyName = nestedProperty [1]

对象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;

$ / code>

现在您可以这样做:

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


I recently upgraded a project from 2.1.0 to Grails 2.4.4

In a variety of places we use bindData to map incoming JSON to domain objects including nested objects. This doesn't appear to work after the upgrade or the notation has changed.

For example

Class Book {
   Author author
   ...
}

Passing in JSON

{ "author.id" : 7 }  

And calling

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

Would previously find author 7 and attach him to the example book. Now it doesn't bind anything. Has anyone else run into this issue or something similar?

Thanks

Edit:

Updating the incoming JSON fixes this problem and is a potential work around:

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

This format makes more sense but unfortunately it wasn't built this way in the first place.

解决方案

This may help you.
For some reason I cant use the default data binder so I came up with this custom data binder.

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;
    }

Now you can do like this:

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

这篇关于升级到2.4.4后,Grails bindData与嵌套对象不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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